django request in template - django

I've enabled the django request processor
TEMPLATE_PROCESSORS = (
"django.core.context_processors.auth",
"django.core.context_processors.debug",
"django.core.context_processors.i18n",
"django.core.context_processors.media",
"django.core.context_processors.request",
)
Still i don't have to request variable available in templates.
I've to manually pass it. Using django 1.0.2
Everywhere on web it seems it's only about enabled request processor..
Also i am using RequestContext as :
return render_to_response(
'profile.html',
{
'persons':Person.objects.all(),
'person':Person.objects.get(id=id),
'request':request,
},
context_instance=RequestContext(request)
)
no luck
ohh darn
the new name for that is TEMPLATE_CONTEXT_PROCESSORS

settings.py:
TEMPLATE_CONTEXT_PROCESSORS = (
# ...
'django.core.context_processors.request',
# ...
)

TEMPLATE_CONTEXT_PROCESSORS
instead of
TEMPLATE_PROCESSORS

Be advised that as of Django 1.8, this has changed to a "TEMPLATES" setting, and in the default configuration, the request processor is NOT included.
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
# insert your TEMPLATE_DIRS here
],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
# Insert your TEMPLATE_CONTEXT_PROCESSORS here or use this
# list if you haven't customized them:
'django.contrib.auth.context_processors.auth',
'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',
],
},
},]
Just add the request processor back in to fix the issue:
'django.core.context_processors.request',
For more info, see the Django Upgrading Docs.

Are you sure you don't have the request variable available to the template? What happens when you remove the line
'request':request,
that's different from when that line is present. If your template loads the same either way, the problem is with your template.

MIDDLEWARE_CLASSES=(
...
'yourfolder.yourfile.yourclass',
...
yourclass:
class AddRequestToTemplate:
process_templaet_response(self, request, response):
response.context_data['request']=request

Related

Django context processors model not found

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.

Django 1.6 Admin Page Overriding Not Working

We're trying to override the admin page for Django 1.6, but it continues to get it from django/contrib/templates/...:
settings.py:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
[os.path.join(BASE_DIR, 'templates')],
],
'OPTIONS': {
'context_processors': [
# Insert your TEMPLATE_CONTEXT_PROCESSORS here or use this
# list if you haven't customized them:
'django.contrib.auth.context_processors.auth',
'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',
],
'loaders': [
# insert your TEMPLATE_LOADERS here
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
]
},
},
]
]
and the files structure:
project
project
templates
app_name
admin
file_to_override <--it varies here from being inside the app_name or inside template itself
I'm not sure why the directories arent working
I don't know why you're using Django 1.6, but the TEMPLATES dict syntax you quote is only for Django 1.8+. In previous versions, you need to specify all the options individually.
Also note you have wrongly surrounded the DIRS value with two list brackets.
TEMPLATE_DIRS = [
os.path.join(BASE_DIR, 'templates'),
]
TEMPLATE_CONTEXT_PROCESSORS = [
'django.contrib.auth.context_processors.auth',
'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',
]
TEMPLATE_LOADERS = [
# insert your TEMPLATE_LOADERS here
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
]

Changing default template for login form in Django 1.5

Very simple question. When using the login_required decorator above a view, the authentication system redirects me to whatever URL is defined in LOGIN_URL in settings.py, using the template found in registration/login.html.
The question: How can I define a different template name for my login form (I don't want to use the default)?
login documentation:
If you’d prefer not to call the template registration/login.html, you can pass the template_name parameter via the extra arguments to the view in your URLconf. For example, this URLconf line would use myapp/login.html instead:
(r'^accounts/login/$',
'django.contrib.auth.views.login',
{'template_name': 'myapp/login.html'}),
----Django 1.10--------
I changed TEMPLATE_DIR in settings to solve this problem
setting.py
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(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',
],
},
},]
urls.py
url(r'^login/$', auth_views.login, {'template_name': 'registration/login.html'}, name='login'),
and place the registration file directory under templates directory!

Passing an object in every Django page

I have an object called Groups that is used in every single page on my website. However, Django only passes in Python objects to html through render_to_response and I can't render to response everytime something happens to the groups object.
How do I maintain this object(as in make it respond to adding and deletion) and produce it in every Django template that I have without calling render_to_response?
write a template context processor:
#my_context_processors.py
def include_groups(request):
#perform your logic to create your list of groups
groups = []
return {'groups':groups}
then add it in your settings file:
#settings.py
TEMPLATE_CONTEXT_PROCESSORS = (
"django.core.context_processors.auth",
"django.core.context_processors.debug",
"django.core.context_processors.i18n",
"django.core.context_processors.media",
"path.to.my_context_processors.include_groups",
)
now a variable groups will be available to you in all your templates
If you need data added to more than one template contexts you should look into achieving that via your own template context processor.
You need to create template context processor to pass an object to each request. Here is some example
#tu_context_processor.py
from setting.models import Business
def include_business(request):
business = Business.objects.all().last()
return {'business': business}
in your settings file:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [TEMPLATE_DIR],
'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',
'core.tu_context_processor.include_business',
],
},
},
]
now a variable business will be available to you in all your templates
tested in Django 4

creating my own context processor in django

I have come to a point where I need to pass certain variables to all of my views (mostly custom authentication type variables).
I was told writing my own context processor was the best way to do this, but I am having some issues.
My settings file looks like this
TEMPLATE_CONTEXT_PROCESSORS = (
"django.contrib.auth.context_processors.auth",
"django.core.context_processors.debug",
"django.core.context_processors.i18n",
"django.core.context_processors.media",
"django.contrib.messages.context_processors.messages",
"sandbox.context_processors.say_hello",
)
As you can see, I have a module called 'context_processors' and a function within that called 'say_hello'.
Which looks like
def say_hello(request):
return {
'say_hello':"Hello",
}
Am I right to assume I can now do the following within my views?
{{ say_hello }}
Right now, this renders to nothing in my template.
My view looks like
from django.shortcuts import render_to_response
def test(request):
return render_to_response("test.html")
The context processor you have written should work. The problem is in your view.
Are you positive that your view is being rendered with RequestContext?
For example:
def test_view(request):
return render_to_response('template.html')
The view above will not use the context processors listed in TEMPLATE_CONTEXT_PROCESSORS. Make sure you are supplying a RequestContext like so:
def test_view(request):
return render_to_response('template.html', context_instance=RequestContext(request))
According to the django docs you can use render as a shortcut instead of render_to_response with the context_instance argument:
Alternatively, use the render() shortcut which is the same as a call to render_to_response() with a context_instance argument that forces the use of a RequestContext.
Let's say you have a file structure like this:
YourDjangoProject
├───project
│ ├───__init__.py
│ ├───asgi.py
│ ├───settings.py
│ ├───urls.py
│ └───wsgi.py
├───.env
├───manage.py
└───db.sqlite3
1) Anywhere, create a context_processors.py file
I'll create one in the project folder (with settings.py):
YourDjangoProject
└───project
├───...
└───context_processors.py
2) Create a function in context_processors.py that accepts a HttpRequest object as an argument and returns a dictionary
A context processor is just a function that accepts an HttpRequest object as an argument and returns a dictionary.
Like this:
# project/context_processors.py
def site_email(request):
return {'site_email': 'example#gmail.com'}
3) Add this to your context_processors setting in settings.py (at the bottom for security reasons)
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [BASE_DIR / 'config' / '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',
'project.context_processors.site_email', # <- New context processor here
],
},
},
]
Now you'll be able to access the 'site_email' template variable on every single django template across your whole site.
Happy coding!
Since Django 1.8 you register your custom context processors like this:
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',
'www.context_processors.instance',
],
},
},
]
assuming your context processor is in app www in context_processors.py
If you’re using Django’s render_to_response() shortcut to populate a template with the contents of a dictionary, your template will be passed a Context instance by default (not a RequestContext). To use a RequestContext in your template rendering, use the render() shortcut which is the same as a call to render_to_response() with a context_instance argument that forces the use of a RequestContext.