---
source: https://iinventedlight.com/library/notes/http-set-cookie.md
title: "Set cookies from Django and Starlette responses"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "BSD-3-Clause (Django docs); BSD-3-Clause (Starlette docs); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "Summary by iinventedlight (AI-assisted). Sources: Django docs, Request and response objects; Starlette docs, Responses. BSD-3-Clause; BSD-3-Clause."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-19
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-19
as_of: 2026-11-19
confidence: medium
untested: "Not reproduced (2026-11-19). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-web: Set-Cookie from Django and Starlette responses"
tags: [http, cookies, set-cookie, django, starlette, samesite, httponly]
client: Librarian
sources:
  - id: S1
    title: "Request and response objects"
    publisher: Django docs
    url: https://docs.djangoproject.com/en/5.2/ref/request-response/
    accessed: 2026-11-19
    license: "BSD-3-Clause"
  - id: S2
    title: "Responses"
    publisher: Starlette docs
    url: https://starlette.dev/responses/
    accessed: 2026-11-19
    license: "BSD-3-Clause"
---
# 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

- [django-csrf](https://iinventedlight.com/library/notes/django-csrf/)
- [django-request-response](https://iinventedlight.com/library/notes/django-request-response/)
- [nextjs-cookies-and-headers](https://iinventedlight.com/library/notes/nextjs-cookies-and-headers/)

## Claims

- id: C1
  claim: Django HttpResponse.set_cookie sets a cookie on the response.
  source: S1
  evidence: "Sets a cookie."

- id: C3
  claim: delete_cookie removes a cookie by key, path, and domain.
  source: S1
  evidence: "Deletes the cookie with the given"

- id: C4
  claim: Starlette's set_cookie method sets cookies on the response object.
  source: S2
  evidence: "set_cookie method to allow you to set cookies on the response object"

## Sources

- [S1] Request and response objects. Django docs. https://docs.djangoproject.com/en/5.2/ref/request-response/ . License: BSD-3-Clause. Accessed 2026-11-19.

- [S2] Responses. Starlette docs. https://starlette.dev/responses/ . License: BSD-3-Clause. Accessed 2026-11-19.

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.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
