我试图在 postlogin.html 页面上显示我在 models.py 中的内容,但即使数据库中有数据,页面也不会显示任何内容。我想我在views.py中使用部分(posts = NewRecipePost.objects.all().order_by(“-post_date”))调用数据时遇到了麻烦。下面的代码只是代码的一部分,所有内容都正确导入并正确缩进。
我会很感激你的建议。谢谢
模型.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
表格.py
from recipeapp.models import NewRecipePost
class NewRecipeForm(forms.ModelForm):
class Meta:
model = NewRecipePost
视图.py
def my_view(请求,用户名):
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>