Django

 

4401280

Introduction & Django Admin

Project Setup

django-admin startproject project
project/
    manage.py
    project/
        __init__.py
        settings.py
        urls.py
        asgi.py
        wsgi.py

Create App blog

python3 manage.py startapp blog

Add blog app to INSTALLED_APPS in settings.py:

INSTALLED_APPS = [
      "django.contrib.admin",
      "django.contrib.auth",
      "django.contrib.contenttypes",
      "django.contrib.sessions",
      "django.contrib.messages",
      "django.contrib.staticfiles",
      "blog",
]

Database Migration and Super user

Database migrations:

python3 manage.py makemigrations
python3 manage.py 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 manage.py 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.

Models

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",)}

admin.site.register(Tag)
admin.site.register(Post, PostAdmin)
admin.site.register(Comment)

Generic Relationships

HTML Frameworks (CSS Frameworks)

Custom Filters

Custom Template Tags

Crispy Forms