I am trying to implement a context_processor in Django via a custom app. I am failing due to Module not found error.
My app name is brand.
My context processer looks like this:
from .models import ContactDetail
def contact(request):
contact = ContactDetail.objects.first()
return { 'contact' : contact }
My app is included in installed apps and the model has been migrated.
I have included my context processor into the context_processors list as per documentation:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'mysite', 'templates'),],
'OPTIONS': {
'context_processors': [
'brand.context_processors.contact',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'django.template.context_processors.i18n',
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.template.context_processors.media',
'django.template.context_processors.csrf',
'django.template.context_processors.tz',
'sekizai.context_processors.sekizai',
'django.template.context_processors.static',
'cms.context_processors.cms_settings',
],
'loaders': [
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
'django.template.loaders.eggs.Loader'
],
},
},
]
The Error I am getting is:
Django Version: 1.11.16
Exception Type: ModuleNotFoundError
Exception Value: No module named 'brand.context_processors'
I have tried searching for solutions but most of the questions are for previous versions of django ( <= 1.8 ) I am using 1.11.16.
I have read through the docs and it is unclear what I am missing.
Any help is apreciated.
Make sure that you named that file context_processors.py and placed in myapp directory.
Related
I am new to django and I am using version 4.1.2. I have created an app with the following structure
I have configured the app ( 'home') template in the primary setting file like the following. but still, I am getting the error
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
os.path.join(BASE_DIR/'home', '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',
],
},
},
]
here is my view code
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def home(request):
return render(request, ' home/welcome.html' , {})
Remove the space in the template path: ' home/welcome.html' have to be 'home/welcome.html'
I'm using django 3.0.
I use django-admin startproject mysite and created a sample django project.
I don't think the sample project has any models so I commented out "django.contrib.contenttypes" in INSTALLED_APPS in settings.py. I also commented out all middlewares.
I then wrote a simple view
from django.shortcuts import render
def index(request):
return render(request, 'hello.html')
and hello.html is just a blank file.
Once I access the page, django throws exception
Model class django.contrib.contenttypes.models.ContentType doesn't
declare an explicit app_label and isn't in an application in
INSTALLED_APPS.
Can anyone help explain the exception? The sample project doesn't have any model, why do I need django.contrib.contenttypes? Can django websites live without django.contrib.contenttypes?
I have to remove django.contrib.auth.context_processors.auth in TEMPLATES.
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [BASE_DIR+"/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',
],
},
},
]
I wanted to add custom error pages to my website, butI am getting the simple pages, for example the 403 error I am not getting the template I built. template is located in templates/http_errors/ folder.
my template settings in settings file looks like this:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [normpath(join(PROJECT_DIR, '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',
'django.template.context_processors.media',
'django.template.context_processors.i18n'
],
},
},
]
and urls.py:
from django.conf.urls import handler403
handler403 = 'core.views.custom_handler403'
and views.py:
def custom_handler403(request):
response = render_to_response('http_errors/HTTP403.html', {},
context_instance=RequestContext(request))
response.status_code = 403
return response
I tried a lot of options found on django docs and stackoverflow but could not still find the answer. It would be great if you helped me with this.
P.S. I am using Django 1.10 and Python 3.5
These custom pages will only work if you have DEBUG = False in your settings. Otherwise normal debug handlers will be used
I have a context processor which makes constants available in templates, including base template.
On my vagrant development server, using runserver, these work fine. The production server uses apache and WSGI.
I'm using Django 1.9.4 on both development and production.
This is the context_processors.py
from .constants import *
def template_constants(request):
return {'collection_name': COLLECTION_NAME,
'website_name': WEBSITE_NAME,}
This is from settings.py
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.contrib.auth.context_processors.auth',
'django.template.context_processors.request',
'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',
'myapp.photos.context_processors.template_constants',
],
},
},
]
Constants are just in a file constants.py in my app, which I import into files which use them. The file has lines such as:
WEBSITE_NAME = "website name"
My views use render which should be including RequestContext
return render(request, 'page.html', template_variables)
I get no error messages, the variables simply don't have a value in the templates on production, so nothing is shown.
Apart from deployment, the code on production and dev is exactly the same.
Things I've tried:
- Adding string value directly into context_processors.py file (eliminating it being a potential issue importing variables from constants.py)
- Going back to Django 1.8 and 1.7 to check if it's something related to a specific version
None of these changes made any difference.
If I install as suggested in django 1.9 docs (https://docs.djangoproject.com/en/1.9/topics/templates/#django.template.backends.jinja2.Jinja2)
with two items in TEMPLATES, the first is the jinja2 backend, the second is the django backend:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.jinja2.Jinja2',
'DIRS': [
PROJECT_ROOT+ "/templates/jinja",
],
'APP_DIRS' : True,
'OPTIONS': {
'environment': 'commshub.jinja2_settings.environment',
},
},
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
PROJECT_ROOT+ "/templates",
],
'OPTIONS': {
'debug': DEBUG,
'context_processors': [
"django.contrib.auth.context_processors.auth",
"django.core.context_processors.debug",
"django.core.context_processors.media",
'django.core.context_processors.request',
'django.core.context_processors.static',
'django.contrib.messages.context_processors.messages',
],
},
},
]
but WITHOUT 'environment' setting, then appears to work provided I remove also 'auto_reload' setting for both jinja and django, otherwise get
__init__() got an unexpected keyword argument 'auto_reload'
If I add myproj.jinja2.py with content as shown in django docs, still no 'environment' option, I get:
cannot import name Environment
If I change "from jinja2 import Environment" to ""from jinja2.environment import Environment"
No module named environment
In light of this post, https://groups.google.com/forum/#!msg/django-users/pytjLOVUwmM/gbiDmswSuq4J, I try renaming jinja2.py to jinja2_settings.py and add this TEMPLATE options:
'environment': 'jinja2_settings.environment', -> pop() takes no arguments (2 given)
'environment': 'myproj.jinja2_settings.environment', -> No module named commshub.jinja2_settings
Tried removing django backend altogether but got the same error as above.
Not sure what else to try!