Django Complete Cheat Sheet.
Get the Full Django Guidebook Here:
codewithdhanian.gumroad.com/…
1. Introduction to Django
* A high-level Python web framework.
* Follows the MVT (Model-View-Template) architectural pattern.
* Emphasizes DRY (Don’t Repeat Yourself) and rapid development.
* Includes an admin panel, ORM, templating engine, and authentication out of the box.
2. Django Setup
* Install Django using pip.
* Start a project using `django-admin`.
* Create apps using `
manage.py startapp`.
* Configure settings in `
settings.py`.
3. Project Structure
* `
manage.py`: CLI tool for project management.
* `
settings.py`: Configuration for the project.
* `
urls.py`: Route management.
* `
asgi.py` & `
wsgi.py`: Interfaces for deployment.
* `apps/`: Modular Django applications.
4. Apps and Configuration
* Each app must be added to `INSTALLED_APPS`.
* Maintain modularity using multiple apps.
* Use `AppConfig` for app-specific configurations.
5. Models
* Define data structure as Python classes.
* Map to database tables via Django ORM.
* Use fields like CharField, IntegerField, DateField.
* Define relationships: OneToOne, ForeignKey, ManyToMany.
* Migrate changes with `makemigrations` and `migrate`.
6. Admin Interface
* Register models in `
admin.py`.
* Use built-in admin to manage database records.
* Customize with model admin classes.
* Requires superuser account to log in.
7. Views
* Handle request/response logic.
* Types: Function-based views (FBV), Class-based views (CBV).
* Return responses like HTML, JSON, or redirects.
* Use mixins and generic views for reusable logic.
8. Templates
* Use Django Template Language (DTL).
* Embed Python-like logic with `{% %}` tags.
* Render variables using `{{ }}`.
* Support template inheritance using `{% extends %}` and `{% block %}`.
* Include static files with `{% static %}`.
9. URL Routing
* Use `path()` or `re_path()` for defining URLs.
* Include app-specific URLs using `include()`.
* Name URLs for reverse lookups.
* Organize with namespaces if needed.
10. Static and Media Files
* Static: CSS, JS, images.
* Media: User-uploaded content.
* Configure `STATIC_URL`, `STATICFILES_DIRS`, `MEDIA_URL`, `MEDIA_ROOT`.
* Use `collectstatic` for deployment.
11. Forms and Validation
* Use `Form` for custom forms.
* Use `ModelForm` to auto-generate from models.
* Add validations using `clean()` methods.
* Render forms in templates and handle POST data.
12. Django ORM
* Abstracts SQL using Python classes.
* Retrieve records using `filter`, `get`, `exclude`, `all`.
* Create, update, delete records using ORM methods.
* Use `Q` and `F` objects for complex queries.
* Optimize with `select_related` and `prefetch_related`.
13. Middleware
* Process requests and responses globally.
* Located in the `MIDDLEWARE` list in `
settings.py`.
* Use built-in middleware or create custom ones.
* Common uses: session management, authentication, security.
14. Authentication and Authorization
* Built-in `User` model and auth system.
* Support for login, logout, password change, and reset.
* Use decorators like `
@login_required`.
* Group users and assign permissions.
* Extend `User` model using `AbstractUser` or `OneToOneField`.
15. Sessions and Messages
* Sessions store per-user data on the server.
* Use session middleware and context processors.
* Messaging framework enables flash messages.
* Display messages in templates.
16. Django REST Framework (DRF)
* Toolkit for building Web APIs.
* Use serializers for converting models to JSON.
* Views: `APIView`, `GenericAPIView`, `ViewSets`.
* Routers for automatic URL routing.
* Support for authentication (Token, JWT), throttling, filtering.
17. Testing
* Write tests using `TestCase` classes.
* Use Django’s test client to simulate requests.
* Load test data with fixtures.
* Run tests using `
manage.py test`.