3

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?

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

3 Comments

Well, I'm just using render_to_response("some.template.file").
The docs include this text: "If you need to use context processors, render the template with a RequestContext instance instead. Your code might look something like this: render_to_response('my_template.html', my_data_dictionary, context_instance=RequestContext(request)) "
Okay that helped me find the answer, thank you very much! A RequestContext instance must be passed explicitly.
1

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)

2 Comments

return render( request, "some.template.file", templateArguments ) is shorter and by default it uses RequestContext.
@hobbes3: Right, that was introduced in 1.3 - I grew up using the old variant. Edited my answer, thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.