Skip to content

context_processors

Context processors are used to extend the context passed to all templates with values which are commonly used.

By default, no additional content is added to the context. In order to make use of context processors, add the context_processors option when configuring the template backend. For example:

TEMPLATES = [
    {
        "BACKEND": "ucam_template_renderer.django.template.Engine",
        "NAME": "external",
        "OPTIONS": {
            "context_processors": [
                "ucam_template_renderer.django.context_processors.admin_urls",
                "ucam_template_renderer.django.context_processors.auth_urls",
                "ucam_template_renderer.django.context_processors.csrf_token",
                "ucam_template_renderer.django.context_processors.user",
            ],
        },
    },
]

You can create your own custom context processors by defining a function which takes an incoming Django request and returns a context dictionary. The context dictionaries returned from context processes will be "deep merged" together with the template context to form the final context.

from django.http import HttpRequest

def my_context_processor(request: HttpRequest):
    return {"someKey": "someValue"}

Functions:

  • auth_urls

    Extend the context with an authUrls dictionary containing login URL.

  • admin_urls

    Extend the context with an adminUrls dictionary containing index URL pointing to admin.

  • csrf_token

    Extend the context with an csrfToken property containing the CSRF token.

  • user

    Extend the context with an user dictionary containing the following properties:

auth_urls

auth_urls(request)

Extend the context with an authUrls dictionary containing login URL.

admin_urls

admin_urls(request)

Extend the context with an adminUrls dictionary containing index URL pointing to admin.

csrf_token

csrf_token(request)

Extend the context with an csrfToken property containing the CSRF token.

user

user(request)

Extend the context with an user dictionary containing the following properties:

  • isAuthenticated
  • isAnonymous
  • email
  • username
  • firstName
  • lastName