"No module named context_processors" After Upgrade - python

I've upgraded Django from version 1.8 to version 1.10 and I am now getting the below error:
No module named context_processors
This is in relation to this code in settings.py. If I comment out the lines that start with 'django.core it runs fine but I obviously will be losing functionality:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
TEMPLATE_PATH,
],
'APP_DIRS': True,
'OPTIONS': {
'debug': DEBUG,
'context_processors': [
...
'django.core.context_processors.debug',
'django.core.context_processors.i18n',
'django.core.context_processors.media',
'django.core.context_processors.static',
'django.core.context_processors.tz',
'django.contrib.messages.context_processors.messages',
...
],
},
},
]
How can I fix this?
Note: This, this and this are all similar but nothing Google returns addresses this issue.

After a lot of digging, I found a fix. Hidden in the documentation is the solution to the issue:
django.core.context_processors
Built-in template context processors have been moved to django.template.context_processors.
Hence, in order to fix the issue, you need to replace django.core with django.template. The code would then look like this:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
TEMPLATE_PATH,
],
'APP_DIRS': True,
'OPTIONS': {
'debug': DEBUG,
'context_processors': [
...
'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',
...
],
},
},
]

Related

TemplateDoesNotExist Error in Pythonanywhere

Erorr
I wanna know where django search for index.html
Setting.py
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ['templates'],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
views.py
def index(request):
return render(request, 'index.html', {})
Django will look in the templates directory, that's if you created a folder called templates and if you have a file named index.html in the templates folder. For some reason I feel like the reason why you are asking this question is because you can not get your template to render when you run the server, am I correct? Please get back to me and I can do my best to fill you in so you can get your project working.
You can easily solve this problem: if the name of your project directory, where manage.py is in, is "RalphPortfolio", you need to make the following correction in settings.py:
'DIRS': ['RalphPortfolio/templates'],
add the full path to the template dirs
for example if your path is /home/some_url/your_project/templates
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ['/home/some_url/your_project_name/templates'],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
it's because the path is not clear well and it will not find it.

After installing jinja2 TemplateDoesNotExist at /admin/

I have installed jinja2 and after that 'DIRS' stopped working(I have to include them by hand).
Changing 'APP_DIRS' doesn`t help
templates look like that:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.jinja2.Jinja2',
'APP_DIRS': False,
'DIRS': ['main/templates', 'shop/templates'],
'OPTIONS': {
'environment': 'django_test.create_jinjia_env.environment',
'autoescape': True,
'auto_reload': DEBUG,
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
If don`t include templates into DIRS it throws the same error
Didn`t find the questions like that. Thanks in advance!
The Django admin app does not come with Jinja templates. If you wish to use Jinja and the admin app, you need to include both engines in your TEMPLATES setting:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True, # This allows Django to find the templates in the admin app
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
{
'BACKEND': 'django.template.backends.jinja2.Jinja2',
# The rest of your Jinja2 settings.
},
Secondly, when APP_DIRS is True, the Jinja2 backend looks for templates in a jinja2 subdirectory. That means you should put your templates in main/jinja2 and shop/jinja2 instead of main/templates and shop/templates.

Django-mobile import error: module does not define a flavour attribute/clsss

Trying to apply django-mobile to build mobile versions of templates. https://pypi.python.org/pypi/django-mobile
Installation is little mess because TEMPLATE_LOADERS were deprecated (Using Django 1.8), so I added loaders below according to django documentation. So that may be the only difference between my code and instruction. After this I had error saying that APP_DIRS shall be removed so I set it false.
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': False,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'django_mobile.context_processors.flavour ',
],
'loaders': [
'django_mobile.loader.Loader',
]
},
},
]
Finally, with such settings I get this error:
ImportError at /user/VitalyKotik/
Module "django_mobile.context_processors.flavour " does not define a "flavour " attribute/class
Exception Location: /Users/TheKotik/glboy1/denv/lib/python3.5/site-packages/django/utils/module_loading.py in import_string, line 29
You have a trailing space after 'django_mobile.context_processors.flavour' (..._processors.flavour ')
Did yo add django_mobile.middleware.MobileDetectionMiddleware and django_mobile.middleware.SetFlavourMiddleware to MIDDLEWARE_CLASSES?

Django AllAuth Warning

I'm new to Django and installed the AllAuth package and every seems to work fine. I followed different tutorials but each time I execute the command python manage.py runserver I receive a warning:
WARNINGS:
?: (1_8.W001) The standalone TEMPLATE_* settings were deprecated in Django 1.8 and the TEMPLATES dictionary takes precedence. You must put the values of the following settings into your default TEMPLATES dict: TEMPLATE_CONTEXT_PROCESSORS.
Here a part of my settings.py:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')]
,
'APP_DIRS': True,
'OPTIONS': {
'debug': DEBUG,
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'sekizai.context_processors.sekizai',
],
},
},
]
TEMPLATE_CONTEXT_PROCESSORS = (
"django.core.context_processors.request",
"django.contrib.auth.context_processors.auth",
"allauth.account.context_processors.account",
"allauth.socialaccount.context_processors.socialaccount",
)
AUTHENTICATION_BACKENDS = (
# Needed to login by username in Django admin, regardless of `allauth`
'django.contrib.auth.backends.ModelBackend',
# `allauth` specific authentication methods, such as login by e-mail
'allauth.account.auth_backends.AuthenticationBackend',
)
Any help is appreciated. Regards
As the warning is suggested,you need to move TEMPLATE_CONTEXT_PROCESSORS settings inside TEMPLATES settings,Like this :
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')]
,
'APP_DIRS': True,
'OPTIONS': {
'debug': DEBUG,
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'sekizai.context_processors.sekizai',
"allauth.account.context_processors.account",
"allauth.socialaccount.context_processors.socialaccount",
],
},
},
]
AUTHENTICATION_BACKENDS = (
# Needed to login by username in Django admin, regardless of `allauth`
'django.contrib.auth.backends.ModelBackend',
# `allauth` specific authentication methods, such as login by e-mail
'allauth.account.auth_backends.AuthenticationBackend',
)
So,all the TEMPLATE_CONTEXT_PROCESSORS will be inside TEMPLATES,with 'context_processors' settings,Thanks.
Django regrouped all TEMPLATE_* vars into TEMPLATES var since Django 1.8+
https://docs.djangoproject.com/en/1.8/topics/templates/
This means you can move this part of the code in your TEMPLATE var:
"allauth.account.context_processors.account",
"allauth.socialaccount.context_processors.socialaccount",
And delete this:
TEMPLATE_CONTEXT_PROCESSORS = (
"django.core.context_processors.request",
"django.contrib.auth.context_processors.auth",
"allauth.account.context_processors.account",
"allauth.socialaccount.context_processors.socialaccount",
)
Final result:
TEMPLATES = [
{
# ...
'OPTIONS': {
# ...
'context_processor': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'sekizai.context_processors.sekizai',
"allauth.account.context_processors.account",
"allauth.socialaccount.context_processors.socialaccount",
]
}
}
]

Invalid BACKEND error when I have the seemingly correct settings -- why?

Running Django 1.8 on a django development server on Ubuntu 14.04.
I get this error:
Invalid BACKEND for a template engine: <not defined>. Check your TEMPLATES setting.
My settings file has:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
and when I do `python manage.py diffsettings I get:
...
TEMPLATES = [{'DIRS': [], 'APP_DIRS': True, 'BACKEND': 'django.template.backends.django.DjangoTemplates',
'OPTIONS':{'context_processors': ['django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages']}},
{'TEMPLATE_DEBUG': 'DEBUG'}]
And I can use it when I go to a shell and import it through django.conf. What gives?
Update:
It may be worth noting I'm running python3.
Oh dear. It seemed to be the fact that there was an extra element tagged on the end, the {TEMPLATE_DEBUG: DEBUG} element in the list. Removed that and it gets past that point now.
This is actually the old way of doing it. The 1.8 way is to have debug: True as a key-word in the dictionary.

Categories