我正在尝试在postlogin.html页面上的models.py中显示我所拥有的内容,但即使数据库中有数据,该页面也不会显示任何内容。我想我在使用views.py中的部分(posts = NewRecipePost.objects.all()。order_by(“ - post_date”))调用数据时遇到了问题。下面的代码只是代码的一部分,所有内容都正确导入并正确缩进。
我很感激你的建议。感谢
models.py
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
def my_view(request,username):
posts = NewRecipePost.objects.all().order_by("-post_date")
if request.method == 'POST':
form = NewRecipeForm(request.POST)
if form.is_valid():
form.save()
else:
form = NewRecipeForm()
variables = RequestContext(request, {'form':form,'username':username, 'posts':posts})
return render_to_response('postlogin.html',variables)
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>
答案 0 :(得分:2)
posts.object_list
不存在。您只能将object_list
与Paginator
实例一起使用,但尚未设置。只需将其更改为{% for post in posts %}
,就可以了。