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.
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})
<p>{{post|pprint}}</p>
while you're hacking your templates and views - MattH 2012-04-03 21:14