模板中的django请求

问题描述 投票:0回答:5

我已启用 Django 请求处理器

TEMPLATE_PROCESSORS = (
    "django.core.context_processors.auth",
    "django.core.context_processors.debug",
    "django.core.context_processors.i18n",
    "django.core.context_processors.media",
    "django.core.context_processors.request",
)

我仍然不必请求模板中可用的变量。我必须手动传递它。使用 Django 1.0.2。网络上的任何地方似乎都只与启用的请求处理器有关。

我也使用

RequestContext
作为:

 return render_to_response(
    'profile.html',
    {
        'persons':Person.objects.all(),
        'person':Person.objects.get(id=id),
         'request':request,
    },
    context_instance=RequestContext(request)
)

没有运气。

哦该死的新名字是

TEMPLATE_CONTEXT_PROCESSORS

django request processor
5个回答
46
投票

设置.py:

TEMPLATE_CONTEXT_PROCESSORS = (
  # ...
  'django.core.context_processors.request',
  # ...
)

12
投票

TEMPLATE_CONTEXT_PROCESSORS 代替 模板_处理器


8
投票

请注意,从 Django 1.8 开始,这已更改为“模板”设置,并且在默认配置中,不包括请求处理器。

TEMPLATES = [
{
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': [
        # insert your TEMPLATE_DIRS here
    ],
    'APP_DIRS': True,
    'OPTIONS': {
        'context_processors': [
            # Insert your TEMPLATE_CONTEXT_PROCESSORS here or use this
            # list if you haven't customized them:
            'django.contrib.auth.context_processors.auth',
            'django.template.context_processors.debug',
            'django.template.context_processors.i18n',
            'django.template.context_processors.media',
            'django.template.context_processors.static',
            'django.template.context_processors.tz',
            'django.contrib.messages.context_processors.messages',
        ],
    },
},]

只需重新添加请求处理器即可解决问题:

'django.core.context_processors.request',

有关更多信息,请参阅 Django 升级文档


1
投票

您确定模板没有可用的

request
变量吗?当你移除线时会发生什么

'request':request,

这与该行存在时不同。如果您的模板以任何一种方式加载都相同,则问题出在您的模板上。


0
投票

中间件_类=( ... '你的文件夹.你的文件.你的类', ... 你的班级:

类AddRequestToTemplate: process_templaet_response(自身,请求,响应): response.context_data['request']=请求

© www.soinside.com 2019 - 2024. All rights reserved.