Set Cookie in Response
A snippet showing how to set cookies in Django responses.
# views.py
from django.http import HttpResponse
def set_cookie(request):
response = HttpResponse("Cookie Set")
response.set_cookie('favorite_color', 'blue', max_age=3600)
return response
Explanation:
-
Use
response.set_cookie('key', 'value', max_age=3600)to set a cookie.
- Category Sessions & Cookies
- Total Views 628
- Last Modified 26 May, 2026
- Tags #cookies #response #set #django
Previous snippet
Delete a Session Variable
Next snippet