tags:

views:

15

answers:

1

Hi Guys, 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.)

+1  A: 

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.)

Daniel Roseman
But that's just in a generic view right? I'm using render_to_response and it shows nothing on the html.. I think I need to define something like TEMPLATE_CONTEXT_PROCESSORS tuple with functions etc., but it looks complicated and I'm searching for the fast, simple and standard solution
apple_pie
ok, found it thanks
apple_pie