Categories
Python Answers

How to set cookies with Python Django?

Spread the love

To set cookies with Python Django, we can use set_cookie and request.COOKIES.

For instance, we write

def view(request):
  response = HttpResponse('blah')
  response.set_cookie('cookie_name', 'cookie_value')

to call response.set_cookie with the cookie name and value.

And then we get the cookie by writing

def view(request):
  value = request.COOKIES.get('cookie_name')
  if value is None:
    # Cookie is not set

to call request.COOKIES.get with the cookie name to get the cookie.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *