Django视图从models.py调用数据并显示数据
我试图在postlogin.html页面上显示models.py中的内容,但即使数据库中有数据,页面也不会显示任何内容。我想我调用views.py中的部分(posts=NewRecipePost.objects.all().order\u by(“-post\u date”))的数据时遇到了问题。下面的代码只是代码的一部分,所有内容都已正确导入并正确缩进 谢谢你的建议。谢谢 models.pyDjango视图从models.py调用数据并显示数据,django,django-models,django-templates,django-views,Django,Django Models,Django Templates,Django Views,我试图在postlogin.html页面上显示models.py中的内容,但即使数据库中有数据,页面也不会显示任何内容。我想我调用views.py中的部分(posts=NewRecipePost.objects.all().order\u by(“-post\u date”))的数据时遇到了问题。下面的代码只是代码的一部分,所有内容都已正确导入并正确缩进 谢谢你的建议。谢谢 models.py from django.db import models class NewRecipePost(mo
from django.db import models
class NewRecipePost(models.Model):
title = models.CharField('title', blank=False, max_length=50 )
post_date = models.DateField(auto_now_add=True)
ingredients = models.TextField('ingredients', max_length=1000)
content = models.TextField('content', max_length=1000)
def __unicode__(self):
return self.title
forms.py
from recipeapp.models import NewRecipePost
class NewRecipeForm(forms.ModelForm):
class Meta:
model = NewRecipePost
views.py
定义我的视图(请求、用户名):
postlogin.html
<ul>
{% for post in posts.object_list %}
<div>{{post.title}}</div>
<div>{{post.post_date}}</div>
<ul>
<div>{{post.ingredients}}</div>
<div>{{post.content}}</div>
</ul>
{% endfor %}
</ul>
{posts.object_list%}
{{post.title}
{{post.post_date}
{{post.components}
{{post.content}
{%endfor%}
发布。对象列表不存在。您只能将object\u list
与尚未设置的Paginator
实例一起使用。只需将其更改为{%post in posts%}
,就可以了
<ul>
{% for post in posts.object_list %}
<div>{{post.title}}</div>
<div>{{post.post_date}}</div>
<ul>
<div>{{post.ingredients}}</div>
<div>{{post.content}}</div>
</ul>
{% endfor %}
</ul>