Django上下文不呈现

时间:2014-06-20 18:35:20

标签: python django templates django-context

我在HTML中有一个Django模板。我想使用上下文将变量传递给此模板。但是,当我渲染模板时,Django使用TEMPLATE_STRING_IF_INVALID设置指定的字符串填充引用此变量的空格(我对此进行了测试)。

这是相关的URLconf:

from django.conf.urls import patterns, url

from users import views

urlpatterns = patterns('',
    url(r'^$', views.users),
    url(r'(?P<pk>\d+)/$', views.userdetail),
)

这是它引用的视图:

from django.template import RequestContext, loader
...
def userdetail(request, pk):
    user = get_object_or_404(User, pk=pk)
    template = loader.get_template('users/userdetail.html')
    context = RequestContext(request, {'user': user})
    return HttpResponse(template.render(context))

我很确定这是由于在指定上下文时出现语法错误,但在查看一小时后我找不到。如果您认为可能相关,我很乐意发布其他代码。谁能发现我的错误?

感兴趣的人的模板:

{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif%}

<h1> You are viewing the page for the individual user {{ user.name }} </h1>

    This user has created the following posts:

    {% for post in user.post_list %}
        <a href="/posts/{{ post.id }}/">{{ post.title }}</a></li>
    {% endfor %}

<p>
Created on {{ user.creation_date }}
</p>

1 个答案:

答案 0 :(得分:1)

OP写道:

  

我的主管刚刚来到并迅速解决了问题。问题是模板有一些预定义的关键字。用户是这些关键字之一,所以django对我在上下文中传递{'user':user}感到不安。更改为{'customuser':user}可避免与django关键字发生冲突并修复此问题。