What is the standard, preferred way to make settings available to templates in Django 1.8? Currently, I define a custom context processor in my project and then reference it in my TEMPLATES OPTIONS setting. I've looked through the docs but didn't find this issue mentioned. This previous Stackoverflow question says to do what I'm doing but it's over two years old and I'm wondering if there's a newer preferred method. If I've learned one thing from recently upgrading Django, it's to do things the way the framework wants you to do them. You'll have fewer problems this way.
Thanks.
# utils/context_processors.py
from profile.models import UserProxy
from conf.settings import base as base_settings
def global_constants(request):
"""Constants that are available to all templates."""
return {
'site_name': base_settings.SITE_NAME,
'media_url': base_settings.MEDIA_URL
}
# myproject/settings.py
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug', # default
'django.template.context_processors.request', # default
'django.contrib.auth.context_processors.auth', # default
'django.contrib.messages.context_processors.messages', # default
'utils.context_processors.global_constants', # project
],
},
},
]
Related
I'm building an app in Django in which I have a layout where I have the navbar and I extend this layout to each page.
In order to have the navbar to receive the proper variables from the view I have to write the same code on each view. There has to be a way to write this code once and affect directly the layout so I won't need to type it multiple times but I haven't found the answer.
How can I write the function one time? Something like a global variable in Django I guess.
I hope I managed to explain myself ok, thanks in advance!
first you create context_processors.py somewher in your app.
then you write the function needed, something like:
from .models imoprt Somemodel
def alltimefunc(request):
something = Somemodel.objects.all()
return something
finally you add it in the settings.py
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',
'your_app_name.context_processors.alltimefunc', #here
],
},
},
]
Here comes context processors handy in these kind of situations. You can write a function and register it with template engine in settings.py file. This way you can write code once and it will affect directly to the layout.
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 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.
I am working with Django 1.8.4 (latest).
I’m stuck with the official Django tutorial steps from: https://docs.djangoproject.com/en/1.8/intro/tutorial02/#customize-the-admin-look-and-feel
I am trying to rename “Django administration" but nothing happens.
I searched for the answer and found the totally same question here: _http://stackoverflow.com/questions/28787823/cant-change-django-admin-template There is now answer.
I did exactly the same as described there and in the tutorial.
As you can see from my repo: https://github.com/legobillyjoe/django-tutorial
the location of my base_site.html is mysite/templates/admin, as suggested in the tutorial.
I have added in settings TEMPLATES = [os.path.join(BASE_DIR, 'templates')], but nothing changes.
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',
],
},
},
]
Any suggestions what I can do in order to make it work?
Did you run the makemessages command after you changed the translation strings? Also the default will only appear if the variables return None types
But actually, there's no need to override any templates for your requirement, in your admin file after your import statements add the following:
admin.site.site_header = _(u"Title")
admin.site.index_title = _(u"Subtitle")
So you have something like
from django.contrib import admin
from django.utils.translation import ugettext as _
admin.site.site_header = _(u"Title")
admin.site.index_title = _(u"Subtitle")
class FooAdmin(admin.ModelAdmin):
etc...
I have a small typography related templatetag library that I use on almost every page. Right now I need to load it for each template using
{% load nbsp %}
Is there a way to load it "globally" for all views and templates at once? Putting the load tag into a base template doesn't work.
There is an add_to_builtins method in django.template.loader. Just pass it the name of your templatetags module (as a string).
from django.template.loader import add_to_builtins
add_to_builtins('myapp.templatetags.mytagslib')
Now mytagslib is available automatically in any template.
It will change with Django 1.9 release.
Since 1.9, correct approach will be configuring template tags and filters under builtins key of OPTIONS - see the example below:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'builtins': ['myapp.builtins'],
},
},
]
Details:
https://docs.djangoproject.com/en/dev/releases/1.9/#django-template-base-add-to-builtins-is-removed
In django 1.7 just replace for from django.template.base import add_to_builtins
In Django 1.9 there is an libraries dictionary of labels and dotted Python paths of template tag modules to register with the template engine. This can be used to add new libraries or provide alternate labels for existing ones.
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',
],
'libraries': { # Adding this section should work around the issue.
'custom_tags' : 'myapp.templatetags.custom_tags',#to add new tags module.
'i18n' : 'myapp.templatetags.custom_i18n', #to replace exsiting tags modile
},
},
},
]