What is the difference between a cookie and a session in django?

A cookie is something that sits on the client’s browser and is merely a reference to a Session which is, by default, stored in your database.

The cookie stores a random ID and doesn’t store any data itself. The session uses the value in the cookie to determine which Session from the database belongs to the current browser.

This is very different from directly writing information on the cookie.

Example:

httpresponse.set_cookie('logged_in_status', 'True')
# terrible idea: this cookie data is editable and lives on your client's computer


request.session['logged_in_status'] = True
# good idea: this data is not accessible from outside. It's in your database.

Leave a Comment