1

I noticed that the django templates already have certain variables passed to it without you having to send any data. For instance, the 'user' variable can be called without sending any 'user' data to the template when rendering.

Is there somewhere where I can find a list of these 'default variables'?

Simeon Visser
  • 118,920
  • 18
  • 185
  • 180
Steven Rogers
  • 1,874
  • 5
  • 25
  • 48

3 Answers3

6

The TEMPLATE_CONTEXT_PROCESSORS setting contains the following values by default (in Django 1.6):

"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"

The above is a list of context processors. A context processor is a function which can add more variables to the context which is passed to each template.

For example, the variable user is added by "django.contrib.auth.context_processors.auth".

Simeon Visser
  • 118,920
  • 18
  • 185
  • 180
  • Thanks, That helps a ton! I'm gonna look those up, but without looking those up, am I right to assume that I have some 'tags' from debug, media, static, and messages as default? (assuming they supply tags)? – Steven Rogers Dec 03 '13 at 23:20
  • @StevenRogers you need to read the documentation or otherwise the source code of those context processors. It's reasonable that the `auth` context processor adds the `user` variable but you'd still need to look that up. – Simeon Visser Dec 03 '13 at 23:21
2

Django comes with a special Context class, django.template.RequestContext, that acts slightly differently than the normal django.template.Context. The first difference is that it takes an HttpRequest as its first argument. Go through the docs for more clearer Idea.

https://docs.djangoproject.com/en/1.6/ref/templates/api/#subclassing-context-requestcontext

Sarfraz Ahmad
  • 1,419
  • 3
  • 15
  • 36
0

Now in Django 4.1, the default context processors and template variables are listed at https://docs.djangoproject.com/en/4.1/ref/templates/api/#context-processors

Just for a quick reference:

Context Processor variables
django.contrib.auth.context_processors.auth user
perms
django.template.context_processors.debug debug
sql_queries
django.template.context_processors.i18n LANGUAGES
LANGUAGE_BIDI
LANGUAGE_CODE
django.template.context_processors.media MEDIA_URL
django.template.context_processors.static STATIC_URL
django.template.context_processors.csrf csrf_token
django.template.context_processors.request request
django.template.context_processors.tz TIME_ZONE
django.contrib.messages.context_processors.messages messages
DEFAULT_MESSAGE_LEVELS
x0lani
  • 41
  • 4