Kevin Wierman

Django Cheat Sheet

The idea here is that I wanted an easy cheat sheet to use when going through the motions of creating yet another Django website.

Checking Django Version Number

python -c "import django; print(django.get_version())"

Creating a Project

django-admin startproject mysite

Database Setup

python manage.py makemigrations
python manage.py migrate

Run Development Server

python manage.py runserver

Starting an App

python manage.py startapp polls

Activating Apps

In: mysite/settings.py

Insert:

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'polls',
)

Database Migration

python manage.py makemigrations polls
python manage.py sqlmigrate polls 0001
python manage.py migrate    

Working in the shell

python manage.py shell

Base Model

from django.db import models


class Question(models.Model):
    question_text = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')
    def __str__(self):              # __unicode__ on Python 2
        return self.question_text

class Choice(models.Model):
    question = models.ForeignKey(Question)
    choice_text = models.CharField(max_length=200)
    votes = models.IntegerField(default=0)

Creating an Admin User

python manage.py createsuperuser

Admin modification

in polls/admin.py

from django.contrib import admin

from .models import Question

admin.site.register(Question) 

With Customization:

from django.contrib import admin

from .models import Question


class QuestionAdmin(admin.ModelAdmin):
    fields = ['pub_date', 'question_text']

admin.site.register(Question, QuestionAdmin)

with fieldsets:

from django.contrib import admin

from .models import Question


class QuestionAdmin(admin.ModelAdmin):
    fieldsets = [
        (None,               {'fields': ['question_text']}),
        ('Date information', {'fields': ['pub_date']}),
    ]

admin.site.register(Question, QuestionAdmin)

Assigning HTML Classes:

from django.contrib import admin

from .models import Question


class QuestionAdmin(admin.ModelAdmin):
    fieldsets = [
        (None,               {'fields': ['question_text']}),
        ('Date information', {'fields': ['pub_date'], 'classes': ['collapse']}),
    ]

admin.site.register(Question, QuestionAdmin)