Why is RequestContext used in this way?

53 views
Skip to first unread message

Andrew Taylor

unread,
Nov 15, 2013, 7:59:55 AM11/15/13
to django...@googlegroups.com

Hi,

I've followed some example code which is is follows:

def index(request):
    context = RequestContext(request)
    context_dict =  {'boldmessage': "I am from the context"}
    return render_to_response('rango/index.html',context_dict, context)

Here RequestContext only has the first argument filled i.e. request.

My questions are:

1. Is RequestContext(request) being used in a kind of dummy way to suck up the context processors for render_to_response?
2. What would render_to_response be doing if has both context_dict AND RequestContext had a second argument?

Sorry if these are stupid questions. I'm new to everything python and Dango, and the kind of person who forgets everything they have ever known when trying to decide which pack of toilet paper to buy in the supermarket.

Andy

Fred Stluka

unread,
Nov 15, 2013, 8:59:37 AM11/15/13
to django...@googlegroups.com
Andy,

RequestContext is being passed in so that it become available
to the template, so that it can do things like {% csrf_token %}
which requires access to the session id.

I'm not sure what would happen if a dictionary were passed to
RequestContext as an optional 2nd argument. I've always
passed the dictionary directly to render_to_response().

--Fred
------------------------------------------------------------------------
Fred Stluka -- mailto:fr...@bristle.com -- http://bristle.com/~fred/
Bristle Software, Inc -- http://bristle.com -- Glad to be of service!
Open Source: Without walls and fences, we need no Windows or Gates.
------------------------------------------------------------------------
> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to django-users...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/9f62f09d-cf92-46e2-893c-17d0a39fe4a5%40googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.

Tom Evans

unread,
Nov 15, 2013, 9:12:50 AM11/15/13
to django...@googlegroups.com
This is explained in the docs:

If you do not pass in a context, a Context will be created from the
dictionary passed in.
If you do pass in a context, the contents of the dictionary passed in
will be merged in to the context.

https://docs.djangoproject.com/en/1.6/topics/http/shortcuts/#django.shortcuts.render_to_response

Cheers

Tom

Andrew Taylor

unread,
Nov 27, 2013, 9:52:52 PM11/27/13
to django...@googlegroups.com, teva...@googlemail.com
Thanks guys this was very helpful
Reply all
Reply to author
Forward
0 new messages