django在模板中的请求

Att*_*ila 21 django processor request

我启用了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",
)
Run Code Online (Sandbox Code Playgroud)

我仍然不需要在模板中请求变量.我要手动传递它.使用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)
)
Run Code Online (Sandbox Code Playgroud)

没运气

哦,这个新名字是 TEMPLATE_CONTEXT_PROCESSORS

Din*_*ngo 41

settings.py:

TEMPLATE_CONTEXT_PROCESSORS = (
  # ...
  'django.core.context_processors.request',
  # ...
)
Run Code Online (Sandbox Code Playgroud)

  • 在Djano的更高版本中,请使用:'django.template.context_processors.request' (4认同)

Att*_*ila 11

TEMPLATE_CONTEXT_PROCESSORS而不是TEMPLATE_PROCESSORS


Mon*_*ard 6

请注意,自Django 1.8起,它已更改为"TEMPLATES"设置,并且在默认配置中,不包括请求处理器.

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',
        ],
    },
},]
Run Code Online (Sandbox Code Playgroud)

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

'django.core.context_processors.request',
Run Code Online (Sandbox Code Playgroud)

有关详细信息,请参阅Django升级文档.