HTTPS, HSTS, and Secure Cookies
Force encrypted, secure connections.
HTTPS, HSTS, and Secure Cookies is a free Django Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Django Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Why HTTPS Matters
Plain HTTP sends passwords and cookies as readable text anyone on the network can grab. HTTPS encrypts the whole conversation between browser and server. 🔐
Redirect HTTP to HTTPS
Turn on SECURE_SSL_REDIRECT so Django bounces any plain HTTP request to its HTTPS version automatically. No more accidental insecure pages.
SECURE_SSL_REDIRECT = TrueTrust the Proxy Header
Behind Nginx, Django needs to know the request arrived over TLS. The SECURE_PROXY_SSL_HEADER tells it which header to trust for that.
SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_PROTO", "https")What HSTS Is
HSTS tells browsers to only ever talk to your site over HTTPS for a set time. Even typing http will be upgraded before any request leaves the browser.
Enable HSTS Carefully
Set SECURE_HSTS_SECONDS to enable HSTS. Start small while testing, since browsers will remember it and refuse plain HTTP until it expires.
SECURE_HSTS_SECONDS = 31536000Extend HSTS Reach
Cover every subdomain and qualify for preload lists with two extra flags. They make HSTS apply broadly and let browsers ship it built in.
SECURE_HSTS_INCLUDE_SUBDOMAINS = True
SECURE_HSTS_PRELOAD = TrueCookies Travel With Requests
Your session and CSRF cookies authenticate users. If they leak over HTTP, an attacker can hijack the session, so they need protection flags too.
Secure the Session Cookie
Set SESSION_COOKIE_SECURE to True so the session cookie is only ever sent over HTTPS, never on an unencrypted connection.
SESSION_COOKIE_SECURE = TrueSecure the CSRF Cookie
Do the same for CSRF protection. With CSRF_COOKIE_SECURE on, the token cookie also refuses to ride along over plain HTTP.
CSRF_COOKIE_SECURE = TrueBlock JavaScript Access
The HttpOnly flag hides the session cookie from JavaScript, so a cross-site script cannot read and steal it. Django sets it on sessions by default.
SESSION_COOKIE_HTTPONLY = TrueLimit Cookie Sharing
The SameSite attribute stops cookies from being sent on cross-site requests, adding a second layer of CSRF defense. Django defaults it to Lax for you.
SESSION_COOKIE_SAMESITE = "Lax"Quick Check
Let us see if the HSTS idea stuck.
Recap: Encrypted End to End
You forced HTTPS, taught browsers to remember it with HSTS, and marked your cookies secure and HttpOnly. Traffic and sessions are now encrypted end to end. ✨
Frequently asked questions
Is the “HTTPS, HSTS, and Secure Cookies” lesson free?
Yes — the full text of “HTTPS, HSTS, and Secure Cookies” is free to read here on the web, and the Django Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Django Academy course, upgrade to CoddyKit PRO.
What will I learn in “HTTPS, HSTS, and Secure Cookies”?
Force encrypted, secure connections. You practise Django Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Django Academy?
No prior experience is required. Django Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “HTTPS, HSTS, and Secure Cookies” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Django Academy lesson?
Yes. Every Django Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- DEBUG, SECRET_KEY, and ALLOWED_HOSTS
- HTTPS, HSTS, and Secure Cookies
- XSS, CSRF, and SQL Injection Defenses
- Running the Deployment Checklist