← All notes

Set cookies from Django and Starlette responses

Answer: Set cookies on the response object. Django provides set_cookie on HttpResponse. Starlette provides a set_cookie method on the response object. The parameters share the same idea: a key, a value, an optional lifetime, path, domain, secure, httponly, and samesite. Use delete_cookie to expire a cookie you already set.

Django

Call set_cookie with key, value, max_age, expires, path, domain, secure, httponly, and samesite. That call sets a cookie. The parameters are the same as in the Morsel cookie object. delete_cookie deletes the cookie with the given key, path, domain, and samesite. Match the path and domain you used when setting it. is_secure returns True if the request is secure, that is, if it was made with HTTPS.

Starlette

The call takes key, value, max_age, expires, path, domain, secure, httponly, samesite (default lax), and partitioned. key is a string that will be the cookie's key. value is a string that will be the cookie's value. max_age is an integer lifetime in seconds. A negative integer or 0 will discard the cookie immediately. expires may be an integer number of seconds until the cookie expires, or a datetime. Starlette also provides a delete_cookie method to manually expire a set cookie.

| httponly | secure | |---|---| | httponly | A cookie flag on the response | | secure | A cookie flag on the response | | samesite | Starlette defaults to "lax" | | max_age | Lifetime of the cookie in seconds | | path / domain | Which URLs receive the cookie |

See also

Claims

Sources

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Django docs, Request and response objects; Starlette docs, Responses. BSD-3-Clause; BSD-3-Clause. Text was condensed and changed from the originals.

License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Django docs, Request and response objects; Starlette docs, Responses. BSD-3-Clause; BSD-3-Clause.

Cite this

Permanent ID: iil:note:t8zje12a

iinventedlight. 2026. "Set cookies from Django and Starlette responses." iinventedlight Library, iil:note:t8zje12a, version of 2026-11-19. https://iinventedlight.com/library/notes/http-set-cookie/. License: CC-BY-4.0.

@misc{iil_note_t8zje12a,
  author = {{iinventedlight}},
  title = {{Set cookies from Django and Starlette responses}},
  howpublished = {iinventedlight Library},
  year = {2026},
  date = {2026-11-19},
  url = {https://iinventedlight.com/library/notes/http-set-cookie/},
  note = {Permanent ID iil:note:t8zje12a. License: CC-BY-4.0.},
}

Markdown

Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).

Extra files beside this note

These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.

All artifact files (JSON index)

For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/http-set-cookie.md.