El modelo User y authenticate()
Verifique las credenciales con el usuario integrado
El modelo User y authenticate() es una lección gratuita de Django Academy en CoddyKit. Esta es la lección 1 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.
You Already Have Users
Django ships a full User model out of the box, so you never write account tables by hand. It stores usernames, hashed passwords, and more. 👤
Where the User Lives
The model sits in django.contrib.auth, an app that is enabled in fresh projects. Import it and the whole auth toolkit comes along.
from django.contrib.auth.models import UserKey User Fields
Each user has a username, plus email, first_name, and last_name. These are the everyday fields you read and display in your app.
Passwords Are Never Plain
Django stores a salted hash of every password, never the raw text. Even you, the developer, cannot read a user's actual password. 🔒
Create a User Safely
Use create_user so the password gets hashed for you. Setting the password by hand would store it in the clear.
User.objects.create_user(username="ada", password="secret123")Meet authenticate()
The authenticate() function checks a username and password against the database for you. It is the heart of every login flow.
from django.contrib.auth import authenticateHow authenticate() Works
Pass it the credentials and it returns the matching user object when they are correct. No hashing or lookups for you to write.
user = authenticate(username="ada", password="secret123")When Credentials Fail
If the username or password is wrong, authenticate() returns None. Always check for that before treating the user as logged in.
if user is None:
print("Invalid credentials")Active Users Only
The is_active flag lets you disable accounts. By default authenticate() refuses to return inactive users, blocking banned logins.
Staff and Superusers
Two extra flags matter: is_staff grants admin-site access, and is_superuser grants every permission. Most users have neither.
Authenticate Is Not Login
Remember: authenticate() only verifies identity. It does not start a session, so the user is not yet logged in after this call.
Quick Check
What does authenticate() return when the password is wrong?
Recap: Users and Identity
You met Django's built-in User model and used authenticate() to verify credentials. Passwords stay hashed, and a failed check gives None. ✅
Preguntas frecuentes
¿La lección «El modelo User y authenticate()» es gratis?
Sí — el texto completo de «El modelo User y authenticate()» 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 «El modelo User y authenticate()»?
Verifique las credenciales con el usuario integrado 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 1 de 4.
¿Cuánto tiempo toma la lección «El modelo User y authenticate()»?
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
- El modelo User y authenticate()
- login, logout y Auth Views
- Registro con UserCreationForm
- login_required y protección de vistas