{{request}} var Empty

时间:2016-06-02 15:53:42

标签: python django templates request render-to-response

我正在运行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

如果在我的VIEW中添加

,我设法解决问题
return render_to_response('admin-users/events.html', {}, context_instance=RequestContext(request))

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

1 个答案:

答案 0 :(得分:3)

使用render方法,它会自动使用RequestContext呈现模板。

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

不建议再使用render_to_response快捷方式。