1
我試圖展示我在model.py中的postlogin.html頁面上有什麼,但即使數據庫中有數據,頁面也不會顯示任何內容。我覺得我無法通過views.py中的部分(posts = NewRecipePost.objects.all()。order_by(「 - post_date」))調用數據。下面的代碼只是代碼的一部分,一切都正確導入並正確縮進。Django從models.py調用數據並顯示它
我很感謝您的建議。感謝
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(請求,用戶名):
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>
它的工作!謝謝! – user1426857