开发者

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 templat开发者_如何转开发e? (e.g. the request.host etc.)


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜