如何在Django 1.10的新TEMPLATES设置中包括默认的TEMPLATE_CONTEXT_PROCESSORS [英] How to include the default TEMPLATE_CONTEXT_PROCESSORS in the new TEMPLATES setting in Django 1.10

查看:196
本文介绍了如何在Django 1.10的新TEMPLATES设置中包括默认的TEMPLATE_CONTEXT_PROCESSORS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将一个项目升级到Django 1.10,它的代码如下:

$ d $$$$$$$ <$ p $$> <$$$ c>来自django.conf.global_settings将TEMPLATE_CONTEXT_PROCESSORS导入为TCP

TEMPLATE_CONTEXT_PROCESSORS = TCP +(
'django.template.context_processors.debug',
'django.template.context_processors.i18n',
' django.template.context_processors.media,
django.template.context_processors.static,
django.contrib.auth.context_processors.auth,
django.contrib.messages。 context_processors.messages',
'django.template.context_processors.request',

据我所知,这是使用早期版本的Django来确保默认上下文处理器的常见模式。



在Django 1.10中 TEMPLATE_CONTEXT_PROCESSORS 已被删除,取而代之的是 TEMPLATES 设置,该设置现在应定义如下: $ b

 模板= [
{
...,
'OPTIONS':{
'context_processors':[
'django.template.context_processors.debug',
...
],
},
},
]

应如何定义 TEMPLATES 设置以正确匹配第一个代码示例的行为,即确保始终包含默认上下文处理器?我是否应该手动添加之前 django.conf.global_settings 中的内容? Django 1.10是否在任何地方都定义了默认值?

解决方案

问题是应如何定义TEMPLATES设置正确匹配第一个代码示例的行为,即确保始终包含默认上下文处理器?



在类似情况下,我的答案是虚拟目录并在其中运行 django-admin startproject foo。然后,我检查了foo / foo / settings.py以查看TEMPLATES的生成值。



这可能无法回答有关应如何设置TEMPLATES的每个问题。但这确实回答了您有关TEMPLATES默认内容的问题。


I'm upgrading a project to Django 1.10 and it has code like the following:

from django.conf.global_settings import TEMPLATE_CONTEXT_PROCESSORS as TCP

TEMPLATE_CONTEXT_PROCESSORS = TCP + (
    'django.template.context_processors.debug',
    'django.template.context_processors.i18n',
    'django.template.context_processors.media',
    'django.template.context_processors.static',
    'django.contrib.auth.context_processors.auth',
    'django.contrib.messages.context_processors.messages',
    'django.template.context_processors.request',
)

As far as I can tell this was a common pattern when using previous versions of Django to ensure that the default context processors.

In Django 1.10 TEMPLATE_CONTEXT_PROCESSORS was removed in favour of the TEMPLATES setting which should now be defined something like this:

TEMPLATES = [
    {
        ...,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                ...
            ],
        },
    },
]

How should the TEMPLATES setting be defined to properly match the behaviour of the first code sample, i.e. ensuring that the default context processors are always included? Should I just manually include whatever was in django.conf.global_settings before? Does Django 1.10 have defaults defined anywhere? Are there any new context processors which should probably be included by default?

解决方案

The question is "How should the TEMPLATES setting be defined to properly match the behaviour of the first code sample, i.e. ensuring that the default context processors are always included? "

My answer, in a similar situation, was to make a dummy directory and run 'django-admin startproject foo' in it. Then I examined foo/foo/settings.py to see the generated value of TEMPLATES.

This might not answer every question about how TEMPLATES should be set. But it does answer your question, about the default contents of TEMPLATES.

这篇关于如何在Django 1.10的新TEMPLATES设置中包括默认的TEMPLATE_CONTEXT_PROCESSORS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆