【问题标题】:{{ request }} var Empty{{ 请求 }} var 空
【发布时间】:2016-06-02 15:53:42
【问题描述】:

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

【问题讨论】:

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


    【解决方案1】:

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

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

    不再推荐使用render_to_response 快​​捷方式。

    【讨论】:

      猜你喜欢
      • 2018-05-19
      • 2013-10-24
      • 1970-01-01
      • 2017-09-05
      • 1970-01-01
      • 1970-01-01
      • 2018-04-02
      • 2020-07-16
      • 2013-08-16
      相关资源
      最近更新 更多