Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get a "debug" variable in my Django template context?

According to this SO post:

How to check the TEMPLATE_DEBUG flag in a django template?

if:

  • A) my settings.py file has:

TEMPLATE_CONTEXT_PROCESSORS = ['django.core.context_processors.debug',...

  • and B) I use a RequestContext (as opposed to a Context)

I should have a "debug" variable to my template context. However, I don't: when I do {{debug}} in a template, it renders as nothing ("").

Is there anything else I'm missing that is necessary to get a "debug" var in the template context?

like image 723
machineghost Avatar asked Jun 13 '12 18:06

machineghost


People also ask

How do I enable debug mode in Django?

The debug mode (DEBUG=True) is turned on by default in the Django framework. It provides a detailed traceback with the local variables to find out the error with the line numbers. The error can be triggered from the view page by setting the value of assert to False in the view file.

How do I declare a variable inside a Django template?

Another approach to declare variables in the template is by using custom template tags. Create a custom template tag files named as custom_template_tags.py . Paste the below code in it. Now inside the HTML template use this custom template tag setvar to define a new variable.

How do I change debug in Django?

Open your settings.py file (or settings_local.py ) and set DEBUG = False (just add that line if necessary). Turning off the Django debug mode will: Suppress the verbose Django error messages in favor of a standard 404 or 500 error page. You will now find Django error messages printed in your arches.


1 Answers

You also need to ensure the request's IP address is in the INTERNAL_IPS in your settings (which you probably don't have set): https://docs.djangoproject.com/en/2.1/ref/templates/api/#django-template-context-processors-debug

like image 149
Mark Lavin Avatar answered Oct 12 '22 09:10

Mark Lavin