如何在Django 1.10的新TEMPLATES設置中包含默認的TEMPLATE_CONTEXT_PROCESSORS

[英]How to include the default TEMPLATE_CONTEXT_PROCESSORS in the new TEMPLATES setting in Django 1.10


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

我正在將項目升級到Django 1.10,它的代碼如下:

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.

據我所知,這是使用以前版本的Django時的常見模式,以確保默認的上下文處理器。

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

在Django 1.10中刪除了TEMPLATE_CONTEXT_PROCESSORS以支持TEMPLATES設置,現在應該定義如下:

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?

如何定義TEMPLATES設置以正確匹配第一個代碼示例的行為,即確保始終包含默認上下文處理器?我應該手動包含之前在django.conf.global_settings中的內容嗎? Django 1.10是否在任何地方定義了默認值?是否有任何新的上下文處理器可能默認包含在內?

2 个解决方案

#1


0  

Unpack TCP before the default context processors if running on python 3.

如果在python 3上運行,請在默認上下文處理器之前解壓縮TCP。

from django.conf.global_settings import TEMPLATE_CONTEXT_PROCESSORS as TCP

# Python 3.6
TEMPLATES = [
    {
        ...,
        'OPTIONS': {
            'context_processors': [
                *TCP,
                'django.template.context_processors.debug',
                ...
            ],
        },
    },
]

On lesser versions

在較小的版本上

for a single template configuration:

對於單個模板配置:

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

TEMPLATE['OPTIONS']['context_processors'] = (
    TCP + TEMPLATE['OPTIONS']['context_processors'])

TEMPLATES = [TEMPLATE, ]

for multiple template configurations:

用於多個模板配置:

TEMPLATES = [...]

for template in TEMPLATES:
    template['OPTIONS']['context_processors'] = (
        TCP + template['OPTIONS']['context_processors'])

#2


0  

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? "

問題是“如何定義TEMPLATES設置以正確匹配第一個代碼示例的行為,即確保始終包含默認上下文處理器?”

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.

在類似的情況下,我的答案是創建一個虛擬目錄並在其中運行“django-admin startproject foo”。然后我檢查了foo / foo / settings.py以查看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.

這可能無法回答有關如何設置TEMPLATES的每個問題。但它確實回答了你的問題,關於TEMPLATES的默認內容。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2016/12/05/7258d4bfd0b866f0ea9e40c03ad0142d.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com