Catalog / Django Cheat Sheet
Django Cheat Sheet
A quick reference guide for Django, covering models, views, templates, forms, and common commands.
Models
Model Definition
Defining a model involves creating a Python class that inherits from Example:
|
Field Types
|
For strings of limited length. |
|
For long text entries. |
|
For integer values. |
|
For floating-point numbers. |
|
For boolean (True/False) values. |
|
For dates (year, month, and day). |
|
For dates and times. |
|
For many-to-one relationships. |
|
For many-to-many relationships. |
Model Methods
|
Saves the current instance. |
|
Deletes the current instance. |
|
Returns a human-readable string representation of the object. |
Views
Function-Based Views
Function-based views are Python functions that take a request object as input and return a response. Example:
|
Class-Based Views
Class-based views are Python classes that inherit from Django’s view classes (e.g., Example:
|
Common Decorators
|
Requires the user to be logged in. |
|
Requires the user to have a specific permission. |
|
Ensures that the view is executed within a transaction. |
Templates
Template Syntax
|
Outputs a variable. |
|
Executes a template tag. |
|
Template comment. |
Common Tags
|
Loops through a list. |
|
Conditional statement. |
|
Extends a parent template. |
|
Includes another template. |
|
Reverses a URL pattern. |
|
CSRF protection token. |
Filters
|
Converts a value to lowercase. |
|
Converts a value to uppercase. |
|
Formats a date. |
|
Provides a default value. |
|
Returns the length of a value. |
Forms
Form Definition
Defining a form involves creating a Python class that inherits from Example:
|
Form Rendering
Forms can be rendered in templates using Example:
|
Field Types
|
For strings of limited length. |
|
For email addresses. |
|
For integer values. |
|
For boolean (True/False) values. |
|
For dates (year, month, and day). |
|
For select inputs. |