How to include TEMPLATE_CONTEXT_PROCESSORS by default in the new TEMPLATES parameter in Django 1.10

I am updating the project to Django 1.10 and it has the following code:

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 processors are context sensitive.

In Django 1.10, TEMPLATE_CONTEXT_PROCESSORS was removed in favor of the TEMPLATES parameter, which should now be defined something like this:

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

How should the TEMPLATES parameter be defined in order to correctly correspond to the behavior of the first code example, i.e. to always enable standard context processors? Should I just manually include everything that was in django.conf.global_settings before? Are there any default values ​​in Django 1.10? Are there any new context processors that should probably be enabled by default?

+6
source share
2 answers

Unpack TCP to the default context processors if it is 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', ... ], }, }, ] 

In smaller versions

for one template configuration:

 TEMPLATE = { ..., 'OPTIONS': { 'context_processors': [ 'django.template.context_processors.debug', ... ], }, } TEMPLATE['OPTIONS']['context_processors'] = ( TCP + TEMPLATE['OPTIONS']['context_processors']) TEMPLATES = [TEMPLATE, ] 

for several template configurations:

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

Question: "How to determine the TEMPLATES parameter to correctly match the behavior of the first code sample, that is, to ensure that standard context processors are always on?"

My answer in a similar situation was to create a dummy directory and run "django-admin startproject foo" in it. Then I looked at foo / foo / settings.py to see the generated TEMPLATES value.

This may not answer every question about how to install templates. But it answers your question about the standard content of TEMPLATES.

0
source

Source: https://habr.com/ru/post/1012906/


All Articles