apple_pie
apple_pie

Reputation: 339

How do I use context_instance in my template

Django newbie here, I'm using

render_to_response('example.html', {
        'error_message': error_message,
        }, context_instance=RequestContext(request))

How do I use the request in the template? (e.g. the request.host etc.)

Upvotes: 1

Views: 436

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 600041

The whole point of the context processors is that they automatically add the elements to the context. So you can just use {{ request.host }} or whatever directly in the template.

Edit after comment No, this has nothing to do with generic views. Generic views act in exactly the same way as your own views that use RequestContext as you show above. If you want to make the request object available automatically in your views all you need to do is to add the code below to your settings.py - hard to see how this could be any quicker.

TEMPLATE_CONTEXT_PROCESSORS = (
    "django.contrib.auth.context_processors.auth",
    "django.core.context_processors.debug",
    "django.core.context_processors.i18n",
    "django.core.context_processors.media",
    "django.contrib.messages.context_processors.messages",
    "django.core.context_processors.request"
)

(This is just the default list of context processors as described in the docs, with the request one added.)

Upvotes: 2

Related Questions