0

这是一个新手的错误,我已经搜索了好几个小时了,没有找到一个工作的解决方案。由于不确定要确切搜索什么,因此我认为部分原因。Django模板不显示数据库内容

我正在研究一个小博客应用程序,我有用户,发布,blogComment模型,这就是全部。

我已经为blogIndex和blogPost撰写了意见,即所有帖子和特定帖子的列表。

blogIndex工作在它所属的视图中,它显示{{post.content}} {{post.author.firstname}}等没有问题。

然而blogPost没有。视图看起来是这样的:

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

的blog_post.html看起来是这样的:

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 %} 

随着blog_index.html相同的代码,我得到的标题,内容适当的上市等。

这是urls.py:

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

我怀疑事情是错误的正则表达式? 我怀疑有什么东西更可能是错误的观点?

这是blogIndex,这是工作的看法,但也许有助于回答:

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

这,最后,在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 %} 

链接可能解决方案或指向我的鼻子被视为狭隘都欢迎输入。

回答

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

应该是:

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

作为一般的辅助手段,可以使用pprint过滤器来检查传递给模板数据是什么,你认为它是。例如。 '当你盗用你的模板和视图时''

{{post | pprint}}}

''。 – MattH 2012-04-03 21:14:55

+0

谢谢,我知道这是明显的。这是一个非常漂亮的技巧!再次,谢谢。 – tomasantonj 2012-04-03 21:25:03

+0

不要把这种错误放在心上,相信我每个人都会遇到这种错误。新鲜的眼睛通常是最好的解决方案,你休息之后或别人的。 – MattH 2012-04-03 21:25:58