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

北战南征 提交于 2019-12-08 21:28:44

问题


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?


回答1:


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

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:


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.



来源:https://stackoverflow.com/questions/40976613/how-to-include-the-default-template-context-processors-in-the-new-templates-sett

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!