문제

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'?

도움이 되었습니까?

해결책

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

다른 팁

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top