0Pricing
Django Academy · Lección

HTTPS, HSTS y cookies seguras

Fuerce conexiones cifradas y seguras

HTTPS, HSTS y cookies seguras es una lección gratuita de Django Academy en CoddyKit. Esta es la lección 2 de 4. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de Django Academy, y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de Django Academy incluye 4 lecciones en total.

Partes de esta lección aún no han sido traducidas y se muestran en inglés.

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 = True

Trust 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 = 31536000

Extend 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 = True

Cookies 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 = True

Secure 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 = True

Block 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 = True

Limit 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. ✨

Preguntas frecuentes

¿La lección «HTTPS, HSTS y cookies seguras» es gratis?

Sí — el texto completo de «HTTPS, HSTS y cookies seguras» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de Django Academy, actualiza a CoddyKit PRO. El curso de Django Academy incluye 4 lecciones en total.

¿Qué aprenderé en «HTTPS, HSTS y cookies seguras»?

Fuerce conexiones cifradas y seguras Practicas Django Academy con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.

¿Necesito experiencia previa para empezar Django Academy?

No se requiere experiencia previa. Django Academy en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 2 de 4.

¿Cuánto tiempo toma la lección «HTTPS, HSTS y cookies seguras»?

La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.

¿Puedo escribir y ejecutar código en esta lección de Django Academy?

Sí. Cada lección de Django Academy incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.

Todas las lecciones de este curso

  1. DEBUG, SECRET_KEY y ALLOWED_HOSTS
  2. HTTPS, HSTS y cookies seguras
  3. Defensas contra XSS, CSRF e inyección SQL
  4. Ejecución de la lista de comprobación de despliegue
← Volver a Django Academy