Introduction & Django Admin

Project Setup

django-admin startproject project

Create App blog

python3 startapp blog

Add blog app to INSTALLED_APPS in


Database Migration and Super user

Database migrations:

python3 makemigrations
python3 migrate

makemigrations: instructions written in python, telling how to modify database schema. (what to do)

migrate: applies instructions generated earlier on database.

Superuser creation:

 python3 createsuperuser
Username (leave blank to use 'azat'): Email address: [email protected] Password: ********************
	Password (again): ********************
  This password is too common.
  Bypass password validation and create user anyway? [y/N]: y
  Superuser created successfully.


from django.db import models
from django.conf import settings
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes.fields import GenericRelation
# Create your models here.

class Tag(models.Model):
  value = models.TextField(max_length=100)
  def __str__(self):
    return self.value 

class Comment(models.Model):
  creator = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
  content = models.TextField()
  # using content type
  content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
  object_id = models.PositiveIntegerField()
  # content_object = GenericForeignKey("content_type","object_id")
  # or simply
  content_object = GenericForeignKey()

  # datatiem fields 
  created_at = models.DateTimeField(auto_now_add=True)
  modified_at = models.DateTimeField(auto_now=True)

class Post(models.Model):
  author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.PROTECT)
  created_at = models.DateTimeField(auto_now_add=True)
  modified_at = models.DateTimeField(auto_now = True)
  published_at = models.DateTimeField(blank=True, null=True)
  title = models.TextField(max_length=100)
  slug = models.SlugField()
  summary = models.TextField(max_length=500)
  content = models.TextField()
  tags = models.ManyToManyField(Tag, related_name="posts")
  # post comments 
  comments = GenericRelation(Comment)

  def __str__(self):
    return self.title

Admin site registration

from django.contrib import admin
from blog.models import Tag, Post, Comment

class PostAdmin(admin.ModelAdmin):
  prepopulated_fields = {"slug":("title",)}, PostAdmin)

Generic Relationships

HTML Frameworks (CSS Frameworks)

Custom Filters

Custom Template Tags

Crispy Forms