Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 试图显示数据库中的数据,但只得到空白页_Python_Django_Python 3.x_Django Templates - Fatal编程技术网

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()
#                                 ^^^^^