Cheatography
https://cheatography.com
Django cheet sheet for learning django
This is a draft cheat sheet. It is a work in progress and is not finished yet.
Models
Code |
What it does |
user = models.ForeignKey(User, on_delete=models.CASCADE, null=True, blank=True) |
User the default django user (import it firstly) |
title = models.CharField(max_length=200) |
Char Field |
description = models.TextField(null=True, blank=True) |
Text Field |
complete = models.BooleanField(default=False) |
Boolean Field |
created = models.DateTimeField(auto_now_add=True) |
Date Time Field |
def __str__(self): return self.title |
The string representation of the model |
class Meta: ordering = ['complete'] |
Ordering the data |
|
File Edits
The file |
The change |
What it does |
settings.py |
APP_Name.apps.APP_NameConfig |
To add the new app to django settings |
urls.py (of the app) |
from django.urls import path |
To import the path |
urls.py (of the app) |
from . import views |
To import the view file |
urls.py (of the app) |
urlpatterns = [ ] |
To add the urls you need |
urls.py (of the project) |
from django.urls import path, include |
Import include |
urls.py (of the project) |
path('', include('App_Name.urls')), |
To add the application urls |
models.py |
from django.contrib.auth.models import User |
To user the default user tabel from django |
admin.py |
from .models import Model_Name |
To import the new created model |
admin.py |
admin.site.register(Model_Name) |
To add the new created model to the admin panel |
|
Commands
Command |
What it does |
python -m venv env |
Create virtual environment |
"./env/Scripts/activate" |
Activate python virtual environment |
pip install django |
To install django |
django-admin startproject Project_Name |
To create the project |
python manage.py startapp APP_Name |
To create an app inside the project |
python manage.py runserver |
To run the server |
python manage.py makemigrations |
Changes in the DB |
python manage.py migrate |
Send the changes to the DB |
python manage.py createsuperuser |
Make an admin user |
|
|
|
|
|