40π
β
This is how to do it:
from django.shortcuts import render
def home(request, template):
response = render(request, template) # django.http.HttpResponse
response.set_cookie(key='id', value=1)
return response
π€Jim
6π
If you just need the cookie value to be set when rendering your template, you could try something like this :
def view(request, template):
# Manually set the value you'll use for rendering
# (request.COOKIES is just a dictionnary)
request.COOKIES['key'] = 'val'
# Render the template with the manually set value
response = render(request, template)
# Actually set the cookie.
response.set_cookie('key', 'val')
return response
π€vmonteco
- [Django]-Logging in Django and gunicorn
- [Django]-Convert Django Model object to dict with all of the fields intact
- [Django]-How to show ALL keys through redis-cli?
5π
The accepted answer sets the cookie before the template is rendered. This works.
response = HttpResponse()
response.set_cookie("cookie_name", "cookie_value")
response.write(template.render(context))
π€James Doe 33
- [Django]-Turn off SQL logging while keeping settings.DEBUG?
- [Django]-Prefetch_related for multiple Levels
- [Django]-Django-queryset join without foreignkey
0π
response = render(request, 'admin-dashboard.html',{"email":email})
#create cookies
expiry_time = 60 * 60 #in seconds
response.set_cookie("email_cookie",email,expiry_time);
response.set_cookie("classname","The easylearn academy",expiry_time);
- [Django]-How to export virtualenv?
- [Django]-Django: How to format a DateField's date representation?
- [Django]-Django Rest Framework Ordering on a SerializerMethodField
0π
You can set cookies and render a template in these ways as shown below. *You must return the object otherwise cookies are not set to a browser different from Django sessions which can set the session id cookies to a browser without returning the object and you can see my answer explaining how to set and get cookies in Django.
render() and set_cookie():
from django.shortcuts import render
def my_view(request, template):
response = render(request, template)
response.set_cookie('name', 'John')
response.cookies['age'] = 27
return response # Must return the object
render_to_string(), HttpResponse() and set_cookie():
from django.template.loader import render_to_string
from django.http import HttpResponse
def my_view(request, template):
rts = render_to_string(template)
response = HttpResponse(rts)
response.set_cookie('name', 'John')
response.cookies['age'] = 27
return response # Must return the object set
- [Django]-Django setting : psycopg2.OperationalError: FATAL: Peer authentication failed for user "indivo"
- [Django]-Update to Django 1.8 β AttributeError: django.test.TestCase has no attribute 'cls_atomics'
- [Django]-Django-reversion and related model
-1π
def index(request, template):
response = HttpResponse('blah')
response.set_cookie('id', 1)
id = request.COOKIES.get('id')
return render_to_response(template,{'cookie_id':id})
π€Manjunath
- [Django]-Django, filter by specified month and year in date range
- [Django]-How to find pg_config path
- [Django]-Writing test cases for django models
Source:stackexchange.com