Python 试图显示数据库中的数据,但只得到空白页
我试图显示数据库中的一些数据。以下是模板:Python 试图显示数据库中的数据,但只得到空白页,python,django,python-3.x,django-templates,Python,Django,Python 3.x,Django Templates,我试图显示数据库中的一些数据。以下是模板: <ul> {% for post in latest_post %} <li>{{ post.id }} : {{ post.post_body }}</li> {% endfor %} </ul> 但我只得到一张空白页。为什么? 以下是我试图显示数据的模型: from django.db import models class Post(models.Model): cre
<ul>
{% for post in latest_post %}
<li>{{ post.id }} : {{ post.post_body }}</li>
{% endfor %}
</ul>
但我只得到一张空白页。为什么?
以下是我试图显示数据的模型:
from django.db import models
class Post(models.Model):
creation_date = models.DateTimeField(null=True)
post_name = models.CharField(max_length=30, null=True)
post_title = models.CharField(max_length=50, null=True)
post_body = models.TextField(max_length=2000, null=True)
post_pass = models.CharField(max_length=100, null=True)
post_IM = models.CharField(max_length=15, null=True)
post_image = models.CharField(max_length=100)
image_width = models.IntegerField(null=True)
image_height = models.IntegerField(null=True)
image_size = models.IntegerField(null=True)
image_sha224 = models.CharField(max_length=28, null=True)
您需要调用模型的
all
方法:
latest_post = models.Post.objects.all()
# ^^^^^
如果要返回非全部结果,则应使用过滤器
阅读更多关于Django doc:的信息,如果你只想了解一些最新的latest_post=models.post.objects.all()[:5]@RajaSimon:你看,我现在遇到了一个新问题,所以我想我删除了这个问题,这个问题还没有解决。非常感谢你的帮助。
latest_post = models.Post.objects.all()
# ^^^^^