22

Is it supposed to be listed in Settings.py automatically or do I have to add it? I am having a ridiculously hard time serving up an image file in development and the docs are too terse for my taste and don't spell things out clearly to me for some reason. I'd like to check to see what my template_context_processors are, but where is it located? It's not in my settings.py file. Do I need to edit this typically?

("django.contrib.auth.context_processors.auth",
"django.core.context_processors.debug",
"django.core.context_processors.i18n",
"django.core.context_processors.media",
"django.core.context_processors.static",
"django.contrib.messages.context_processors.messages")
user798719
  • 9,619
  • 25
  • 84
  • 123

2 Answers2

45

In your settings.py you can define TEMPLATE_CONTEXT_PROCESSORS setting.

However, django has defined default values for this setting which is

("django.contrib.auth.context_processors.auth",
"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")

If you want to add your custom template context processor which maintaining the default processors, you can do following in settings.py

import django.conf.global_settings as DEFAULT_SETTINGS

TEMPLATE_CONTEXT_PROCESSORS = DEFAULT_SETTINGS.TEMPLATE_CONTEXT_PROCESSORS + (
    'custom_context_processors.my_context_processor',
)

Refer TEMPLATE_CONTEXT_PROCESSORS doc.

Rohan
  • 52,392
  • 12
  • 90
  • 87
  • Thank you very much. I created a new Django project and do not see the TEMPLATE_CONTEXT_PROCESSORS pre-filled in in the auto-created settings.py file. To serve static files, do I need to copy and paste the above or am I going down the wrong path here? – user798719 Mar 16 '13 at 16:49
  • @user798719, I don't think so. That setting is by default, you don't need to edit it. Also, don't think this is required to serve static files. – Rohan Mar 16 '13 at 17:40
  • Thanks. One additional question. If I want to add a new custom context processor, can I append in some way this to the default context processors or do I need to copy the full context processors configuration on my settings.py? – Miquel Aug 29 '13 at 08:33
  • Thanks. Well explained. – Frankline Nov 25 '14 at 06:03
3

You can check what context processors your app is using by jumping into the django python shell and importing your settings.

$ manage.py shell
> from django.conf import settings
> settings.TEMPLATE_CONTEXT_PROCESSORS

If you have not overridden them then the defaults should be rendered.

On static files, check your STATICFILES_DIRS which is where django's development server will look to serve static assets: https://docs.djangoproject.com/en/dev/ref/settings/#std:setting-STATICFILES_DIRS.

I use this in my settings.py:

from os.path import join, abspath
PROJECT_ROOT = abspath(join(dirname(__file__), '..', '..'))
STATICFILES_DIRS = [join(PROJECT_ROOT, 'public'), ]

This won't be the same for you as it will depend on how you layout your project.

krak3n
  • 948
  • 6
  • 13