作为Django项目的一部分,我具有以下代码(html模板)和views.py文件,但是运行服务器时,浏览器中未显示数据库中的项目。它只是显示一个带有三个项目的空列表。
我肯定在数据库中至少有三个项目,如下所示:
>>> from worldguestbook.models import GuestBookItem
>>> GuestBookItem.objects.all()
<QuerySet [<GuestBookItem: GuestBookItem object (1)>, <GuestBookItem: GuestBookItem object (2)>, <GuestBookItem: GuestBookItem object (3)>]>
>>>
html文件代码(这显示了html模板代码的底部。错误是否可能是html的排列方式?)
<div id="mce-responses" class="clear">
<div class="response" id="mce-error-response" style="display:none"></div>
<div class="response" id="mce-success-response" style="display:none"></div>
</div>
<div class="" style="position: absolute; left: -5000px;"><input type="text" name="b_410ed4e009d15301d90f6492b_753384883a" value=""></div>
</form>
<span class="form_nospam">Warning: The world will see your message</span>
</div>
<!--End mc_embed_signup-->
</div>
</div>
<!-- /end of Row-->
</div>
<!-- End of container-->
<ul>
{% for guestbookitem in all_items %}
<li> {{ GuestBookItem.content }} </li>
{% endfor %}
</ul>
</body>
</html>
views.py代码
from django.shortcuts import render
from django.http import HttpResponse
from .models import GuestBookItem
# Create your views here.
def worldguestbookView(request):
allguestbookitems=GuestBookItem.objects.all()
return render(request, 'worldguestbook\worldguestbook.html',{'all_items' : allguestbookitems})
def loginView(request):
return render(request, 'worldguestbook\login.html')
models.py
from django.db import models
# Create your models here.
class GuestBookItem(models.Model):
content=models.TextField()
注意:服务器在运行时不会引发任何异常/错误,因此我发现很难进行故障排除。
可以将<li> {{ GuestBookItem.content }} </li>
替换为<li> {{ guestbookitem.content }} </li>