Django Authentication
Use Django’s configured user model rather than hard-coding an import that blocks a future custom model. Authentication checks credentials; it does not decide every permission.
How it works
The fixtures create users without usable passwords. This example assigns a generated temporary password for a local authentication check and never prints it. create_user and set_password hash a password; assigning raw text to the password field does not. An application also needs secure transport, appropriate password validation and abuse controls. A successful login alone is not permission to edit another person’s reservation.
Run in the supplied local Django project
This lesson uses Django 5.2.18 LTS with Python 3.12. Download the source downloader and run node download-project.mjs http://127.0.0.1:8000 with Node 22 while previewing locally. It creates a new bhylu-django-lab folder and refuses to overwrite an existing one. After deployment, the default base is bhylu.com; until then use your local preview URL.
Inside that new folder, create a virtual environment, install requirements.txt and run the complete lesson file shown below. Every lesson creates a fresh in-memory SQLite database and temporary upload storage. It does not edit the interactive lab’s db.sqlite3 or Bhylu’s database. The visible listing below is the lesson’s core code; its complete downloaded file includes the shared bootstrap.
Django does not run in Bhylu’s current browser playground. No Apache, MySQL or global PHP configuration change is needed. See the setup lesson for the full project and commands.
Example
Run .venv\Scripts\python.exe lessons\authentication.py from the downloaded project root. Download the complete lesson file; keep it under lessons/. The project’s shared models, forms, views and fixture are prerequisites, not a hidden online runtime.
import secrets
from django.contrib.auth import authenticate, get_user_model
user = get_user_model().objects.get(username="alex")
password = secrets.token_urlsafe(24)
user.set_password(password)
user.save(update_fields=["password"])
print(authenticate(username="alex", password=password).pk == user.pk)
print(authenticate(username="alex", password="wrong") is None)
Expected result
True True
The accepted and rejected credentials are both tested. Only the generated lab process knows the valid password, and the in-memory user disappears when it ends. Use an interactive createsuperuser command for your own local admin account.
Use Django’s versioned official documentation for exact API contracts and the deployment checklist before a separate release.