1

我有一个 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>
4

1 回答 1

1

OP写道:

我的主管刚过来,很快就修好了。问题是模板有一些预定义的关键字。User 是这些关键字之一,所以 django 对我{'user':user}在上下文中传递它感到不安。更改为{'customuser':user}避免与 django 关键字冲突并修复此问题。

于 2015-06-06T17:28:17.170 回答