Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django doesn't create a {{ debug }} variable in DEBUG mode

As of the documentation, there should be variables called "debug" and "sql_queries" usable in templates if all requirements are met.

I have set the following (and checked their values with the debug toolbar):

  • DEBUG = True
  • TEMPLATE_DEBUG = True
  • TEMPLATE_CONTEXT_PROCESSORS left at its default value (containing 'django.core.context_processors.debug')
  • INTERNAL_IPS = ('127.0.0.1',) (and the debug toolbar shows REMOTE_ADDR = '127.0.0.1' under "HTTP Headers")
  • TEMPLATE_STRING_IF_INVALID = "(invalid variable '%s'!)"

When rendering a template containing {{ sql_queries }} {{ debug }}, I get (invalid variable 'sql_queries'!) (invalid variable 'debug'!) as output.

My Django version is 1.2.3. What am I missing here?

like image 420
AndiDog Avatar asked Jun 13 '26 22:06

AndiDog


2 Answers

In your view, are you creating a Context, or a RequestContext? It needs to be RequestContext.

like image 70
Ned Batchelder Avatar answered Jun 16 '26 11:06

Ned Batchelder


Ned Batchelder's answer led me to the right direction. A RequestContext instance must be explicitly passed when using render_to_response:

return render_to_response("some.template.file",
                          templateArguments,
                          context_instance = RequestContext(request))

From Django 1.3, you can use the render function as shorthand:

return render(request, "some.template.file", templateArguments)
like image 39
AndiDog Avatar answered Jun 16 '26 13:06

AndiDog