Django cheat sheet
During the development of the homepage I created a simple cheatsheet for all the django commands which one uses frequently and thought about sharing it:
Start Server
$ python manage.py runserver
Run Tests
$ python manage.py test
$ python functional_tests.py
Create App
$ python manage.py startapp <APPNAME>
- Aftere adding a new app add app to settings.py in your project folder
- add
<APPNAME>.apps.<APPNAME>Config
toINSTALLED_APPS
list - create views in your views.py app
- class based or function based
from django.shortcuts import render
from django.views.generic import ListView
from .models import <MODELNAME>
# function based
def home(request):
return render(request, 'home/home.html', {'title': 'Home'})
# class based
class PostListView(ListView):
model = Post
template_name = 'home/blog.html' # <APPNAME>/<MODELNAME>_<VIEWTYPE>.html
context_object_name = 'posts'
- create folder
templates/<APPNAME>
in you app folder and create your html file intemplates/<APPNAME>/
- create your css files in
static/<APPNAME>/
- reference the url in your
<APPNAME>/urls.py
or<PROJECTNAME>/urls.py
with a url pattern
Create standard databases
$ python manage.py migrate
Create super user
$ python manage.py createsuperuser
- login into
localhost/admin
Add custom database in models.py
- Create classes
Classname(models.Model)
Classname(models.Model)
is your new table in your db after that create fields
$ python manage.py makemigrations
$ python manage.py migrate
- don't forget to register you model in
<APPNAME>/admin.py
from django.contrib import admin
from .models import <MODELNAME>
admin.site.register(<MODELNAME>)
Show SQL Code for Migration
$ python manage.py sqlmigrate <APPNAME> <MIGRATIONNUMBER>
$ python manage.py sqlmigrate home 0001
Run Django Shell
$ python manage.py shell
Interact with Django Objects
from home.models import Post
from django.contrib.auth.models import User
# Get all users
User.objects.all()
# Get first user
User.objects.first()
# Filter first users by field
user = User.objects.filter(username='Blind0ne').first()
# Get user id/pk
user1.id
user1.pk
# Create Entry into the database table
post_1 = Post(title='Blog 1', content='My first Content', author=user)
post_1.save()
Post.objects.all()
Register created databases in admin.py
from django.contrib import admin
from .models import Post
admin.site.register(Post)
Creating forms with html in django
Each form must have a token {% csrf_token %}
Add custom form
Create file <APPNAME>/forms.py
Variable to check if user is logged in
from django.contrib.auth.decorators import login_required
Use it as a decorator for functions @login_required
Add crispy-forms to make it look better
$ pip install django-crispy-forms
- add in settings.py
INSTALLED_APPS='crispy_forms'
- and define variable
CRISPY_TEMPLATE_PACK = 'bootstrap4'
- load crispy form tags in your html
{% load crispy_forms_tags %}
Add signals in signals.py
Signals help you to create objects if other objects are created like create a profile if a user is created.
from django.db.models.signals import post_save
from django.contrib.auth.models import User # SENDER
from django.dispatch import receiver # RECEIVER
from .models import Profile
@receiver(post_save, sender=User)
def create_profile(sender, instance, created, **kwargs):
if created:
Profile.object.create(user=instance)
@receiver(post_save, sender=User)
def save_profile(sender, instance, **kwargs):
instance.profile.save()
Add method to <APPNAME>/apps.py
def ready(self):
import users.signals