Django MOC
Hub for how Django works: models and the ORM, URLs and views, users and the admin, forms, and the commands that drive a project. Security-specific notes live in Web Application and Django Security MOC.
Core concepts
Models & ORM
- Django null Is a Database Setting, blank Is a Validation Setting:
nullacts on the column,blankon validation; strings useblank=Trueand"", other optional fields need both, andunique=True, blank=Truestrings neednull=True.
URLs & views
- Django extra_context Is Evaluated Once, get_context_data on Every Request: class attributes run once per process; per-request values belong in
get_context_data(). - Django Path Converters Validate and Type URL Segments Before the View Runs:
str,int,slug,uuidandpathdecide whether a pattern matches and the type passed as a keyword argument;re_pathnamed groups; existence and permission checks still belong in the view. - Query String Values Arrive in request.GET as Strings, and Missing Keys Raise Unless You Use get():
QueryDict,.get()and.getlist(), converting and validating filters. - Django's APPEND_SLASH Redirect Cannot Carry POST Data:
CommonMiddleware's 301, theRuntimeErrorin debug, lost form data in production.
Database
- PostgreSQL 15 Removed CREATE on the public Schema from Ordinary Users: why
GRANT ALL ON DATABASEno longer letsmigratecreate tables, and making the app user the owner.
Users & admin
- Django Custom User Model Must Exist Before the First Migration:
AbstractUsersubclass plusAUTH_USER_MODELbefore the firstmigrate, thensettings.AUTH_USER_MODELandget_user_model()everywhere. - Customizing Django Admin Enhances User Management: subclassing
UserAdminto show built-in and custom fields (age) in the user list and forms.
Forms
- ModelForm Enhances Data Handling and Validation in Django: form fields and validation generated from the model, with
Meta.fieldschoosing what users may submit.
Testing
- Django's Test Case Classes Differ by How Much Database and Server They Set Up:
SimpleTestCase,TestCase,TransactionTestCase,LiveServerTestCase;on_commitin tests.
Project hygiene
- Dependency Pruning Improves Django Project Security and Maintainability: removing unused Python packages to shrink the attack surface.
Sources (02 - Sources/)
- Django for Beginners - Custom User Model: William S. Vincent's chapter on starting a project with a custom user model.
- How To Set Up Django with Postgres, Nginx, and Gunicorn on Ubuntu 22.04: DigitalOcean's VPS deployment walkthrough, saved to read.
Reference (05 - SOPs & Manuals/)
- Django CLI Cheat Sheet:
django-admin,manage.py, migrations, data migrations and database shells. - Django ORM Query Cheat Sheet:
get/filter/exclude, field lookups, creating users withcreate_user(),update()and printing a QuerySet's SQL. - Start a Django Project with uv:
uv init,startproject core ., a first app, view, template and route, Makefile and.gitignore. - Deploy a Django App on a VPS with PostgreSQL and Gunicorn: packages, database owner, Gunicorn under systemd behind Nginx.
- PostgreSQL Cheat Sheet: psql, roles and databases, a local server with Docker Compose.
- Set Up Tailwind CSS and DaisyUI in Django with django-tailwind:
tailwind init/install/dev, browser reload only underDEBUG, andtailwind buildbeforecollectstatic. - Setup django-debug-toolbar in a dockerized app:
INTERNAL_IPSacross Docker bridge networks.
Related maps
- Web Application and Django Security MOC: XSS, form abuse, rate limiting and other Django security notes.
- Docker and Container Security MOC: the container stack Django runs in.
- Programming Languages MOC: Python language notes.
- Software Testing MOC: testing across frameworks.
References
- Django documentation - the official reference for every note above.