Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save codingforentrepreneurs/3de1f73fa70e7061443688f14be69ec9 to your computer and use it in GitHub Desktop.
Save codingforentrepreneurs/3de1f73fa70e7061443688f14be69ec9 to your computer and use it in GitHub Desktop.
Django: Custom Context Processors

Django Custom Context Processors Example

In cfehome/context_processors.py add:

def my_context(request):
    # caching
    return {
        "my_var": "hello world"
    }

Replace cfehome with any given Django app and/or configuration module.

Update settings.py to:

TEMPLATES = [
    {
        ...
        'OPTIONS': {
            'context_processors': [
                ...
                'cfehome.context_processors.my_context',
            ],
        },
    },
]

In any of your Django templates, you can now use my_var as you see fit:

{% extends "base.html" %}

{% block content %}

{{ my_var }}

{% endblock %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment