Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: use render_to_response and set cookie

Currently, I'm using render_to_response(template_name, locals(), context-etc..)

Trying to set a cookie right now and I'm wondering if I can do it with render_to_response.

All the examples I see are using HttpResponse object. They set the cookie in the response object, like this

response = HttpResponseObject(html)
response.set_cookie("favorite_color",request.GET["favorite_color"])
return response

Wondering if I can set cookie with render_to_response, so I can continue using locals()

Thank you. David.

like image 366
David Avatar asked Feb 13 '11 00:02

David


People also ask

Does Django use cookies by default?

Django uses a cookie containing a special session id to identify each browser and its associated session with the site. The actual session data is stored in the site database by default (this is more secure than storing the data in a cookie, where they are more vulnerable to malicious users).

What is request Meta in the request object?

META contains all the metadata of the HTTP request that is coming to your Django server, it can contain the user agent, ip address, content type, and so on.

What happens when URL py file is edited while the development server is still running?

What happens when url.py file is edited while the development server is still running? Development server terminates.


1 Answers

Yes, not a problem. The principle is exactly the same.

response = render_to_response(template_name, locals(), context-etc..)
response.set_cookie("favorite_color",request.GET["favorite_color"])
return response
like image 126
Wolph Avatar answered Sep 20 '22 05:09

Wolph