I’m trying to show what I have in models.py on the page postlogin.html but even if there’s data in the database, the page doesn’t show anything. I think I’m having trouble calling the data with the part(posts = NewRecipePost.objects.all().order_by(“-post_date”)) in views.py. the code below is only part of the code and everything is correctly imported and correctly indented.
I’d appreciate your advice. Thanks
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>
posts.object_listdoesn’t exist. You only useobject_listwith aPaginatorinstance, which you haven’t set up. Just change it to{% for post in posts %}and you’re good.