Django template not showing database contents

Go To StackoverFlow.com

0

This is for certain a newbie mistake, I have googled for plenty of hours now, not finding a working solution. I assume part due to not being sure what to search for exactly.

I am working on a tiny blog application, I have models for user, post, blogComment and that is all.

I have written views for blogIndex and for blogPost, that is a listing of all posts and a listing of specific posts.

The blogIndex is working in the view that it belongs to, it shows {{ post.content }} {{ post.author.firstname }} and so on with no problem.

The blogPost is however not. The view looks like this:

def blogPost(request, postID):
    blogPost = post.objects.get(id=postID)
    return render_to_response("blog_post.html", {"post":post})

The blog_post.html looks like this:

title: {{ post.title }}</br>
content: {{ post.content }}</br>
datetime: {{ post.datetime }}</br>
author: {{ post.author.first_name }} {{ post.author.last_name }}</br></br>

{% for comment in post.blogcomment_set.all %}
   Comment.firstName: {{comment.firstName}}</br>
   {{comment.lastName}}</br>
   {{comment.email}}</br>
   {{comment.datetime}}</br>
   {{comment.content}}</br>
{% endfor %}

With the same code in blog_index.html, I get a proper listing of title, content and so on.

This is from urls.py:

url(r'^blog/$', blogIndex),
url(r'^blog/(?P<postID>\d+)$', blogPost),

I suspect something is wrong with the regex? I suspect something is more likely wrong with the view?

This is the view for blogIndex, which is working, but maybe helps with answers:

def blogIndex(request):
    posts = post.objects.all()
    return render_to_response("blog_index.html", {"posts":posts})

This, finally, is the code in blog_index.html:

{% for post in posts %}
<h3><a href="/blog/{{ post.id }}">{{ post.title }}</a></h3>
{{ post.content }}
<p>Posted by: {{ post.author.first_name }} {{ post.author.last_name }}<br /> At:    
{{ post.datetime }}</p>     

<p><strong>Comments: </strong><br />
    {% for comment in post.blogcomment_set.all %}
    By: {{ comment.firstname }}<br />
    Title: {{ comment.title }},<br />
    Content: {{ comment.content }}<br />
    Date: {{ comment.datetime }}</p>
    {% endfor %}  
{% endfor %}

Links to possible solutions or pointing me on the nose for being narrow sighted are both welcome input.

2012-04-03 21:05
by tomasantonj


4

def blogPost(request, postID):
    blogPost = post.objects.get(id=postID)
    return render_to_response("blog_post.html", {"post":post})

Should be:

def blogPost(request, postID):
    blogPost = post.objects.get(id=postID)
    return render_to_response("blog_post.html", {"post":blogPost})
2012-04-03 21:08
by MattH
As a general aid, you can use the pprint filter to check data passed to the template is what you think it is. E.g. <p>{{post|pprint}}</p> while you're hacking your templates and views - MattH 2012-04-03 21:14
Thanks, I knew it was something obvious. That is a pretty nifty trick! Again, thanks - tomasantonj 2012-04-03 21:25
Don't take this kind of mistake to heart, believe me it happens to everyone. Fresh eyes are normally the best solution, yours after a break or someone else's - MattH 2012-04-03 21:25
This is why it's better to follow PEP8 and use InitialCaps for model names, but lower_case for instances - Daniel Roseman 2012-04-04 07:13