Django Context Processors Use a context processor to access settings.DEBUG in templates

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Example

in myapp/context_processors.py:

from django.conf import settings

def debug(request):
  return {'DEBUG': settings.DEBUG}

in settings.py:

TEMPLATES = [
    {
        ...
        'OPTIONS': {
            'context_processors': [
                ...
                'myapp.context_processors.debug',
            ],
        },
    },
]

or, for versions < 1.9:

TEMPLATE_CONTEXT_PROCESSORS = (
    ...
    'myapp.context_processors.debug',
)

Then in my templates, simply:

 {% if DEBUG %} .header { background:#f00; } {% endif %}
 {{ DEBUG }}


Got any Django Question?