1

我正在运行 Django 1.9.6 如果我尝试{{ request.path }}在我的模板中使用 var/tag 为空

这是我的settings.py

TEMPLATES = [
{
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': [
        os.path.join(BASE_DIR, '_templates'),
    ],
    'APP_DIRS': True,
    'OPTIONS': {
        'context_processors': [
            'django.template.context_processors.debug',
            'django.template.context_processors.request',
            'django.contrib.auth.context_processors.auth',
            'django.contrib.messages.context_processors.messages',
            'django.template.context_processors.i18n',
        ],
        'builtins': ['django.templatetags.i18n']
    },
},

]

我读到在 1.8 版之后你只需要包含'django.template.context_processors.request',你就会在你的模板中运行 var

如果在我的视图中添加,我设法解决了这个问题

return render_to_response('admin-users/events.html', {}, context_instance=RequestContext(request))

我做错了什么,因为我读了很多答案说我不必添加RequestContext

4

1 回答 1

3

改用该render方法,它会自动使用 aRequestContext来呈现模板。

 return render(request, 'admin-users/events.html', {}) 

render_to_response不再推荐快捷方式。

于 2016-06-02T16:06:04.353 回答