How do I make the DEBUG variable in my settings.py available in my templates in Django?
Without having to go through the dictionary in render_to_respon开发者_运维问答se?
Django has this built into the django.core.context_processors.debug
context processor. So you just need to add that to your TEMPLATE_CONTEXT_PROCESSORS
setting in your settings.py
. This adds the debug
context variable to all views using request context, and the source looks like this:
def debug(request):
"Returns context variables helpful for debugging."
context_extras = {}
if settings.DEBUG and request.META.get('REMOTE_ADDR') in settings.INTERNAL_IPS:
context_extras['debug'] = True
from django.db import connection
context_extras['sql_queries'] = connection.queries
return context_extras
精彩评论