6 views
Skip to first unread message

shivam sharma

unread,
Aug 10, 2018, 5:37:59 PM8/10/18
to django...@googlegroups.com
Can someone explain me difference between Context object and RequestContext obj.
And also difference between 
render_to_response() and render()

Bill Freeman

unread,
Aug 11, 2018, 8:10:10 AM8/11/18
to django-users
A context contains the variables that you want your template to be able to access.  It is common to want to access stuff from the request.  You could copy those things that you need into the dict that you pass to the Context constructor or define an element of the dict to hold the Request object and let the template dig that stuff out..  RequestContext initializes the Context according to the dictionary you pass, but also runs you configured set of context processors, which can paw through the Request object and add values to your Context's dictionary(s).  See: https://docs.djangoproject.com/en/2.1/ref/templates/api/

render() turns your template (and context) into a string representing the body of the response.  render_to_response() does that and uses it to initialize an HttpResponse object that contains that, plus all suitable response headers, some defaulted, like content type and content length, override-able by you, and any additional headers you choose to add, and also the response status, 200 by default, and corresponding status string, which you can override if needed.  You need an HttpResponse object to return from a view so that Django can build the whole response.

Of course, all this is in the documentation.

--
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 https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAKd5AUUPToxjMeg%2B47ENB3T5KQ%2BXyRVb%2BNKnGLKsGz10OyRFPg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages