Wednesday, 7 January 2026

Django Module #5 to #10

Django Module 5 – Models

Module 5 – Models & Database

Create Model


from django.db import models

class Post(models.Model):
    title = models.CharField(max_length=100)
    content = models.TextField()

    def __str__(self):
        return self.title

Migrations


python manage.py makemigrations
python manage.py migrate

Module 6 – Django Admin


from django.contrib import admin
from .models import Post

admin.site.register(Post)

python manage.py createsuperuser

Module 7 – Forms & CRUD


from django import forms
from .models import Post

class PostForm(forms.ModelForm):
    class Meta:
        model = Post
        fields = ['title', 'content']

from django.shortcuts import render, redirect

def create_post(request):
    form = PostForm(request.POST or None)
    if form.is_valid():
        form.save()
        return redirect('/')
    return render(request, 'blog/form.html', {'form': form})

Module 8 – Authentication


from django.contrib.auth import views as auth_views

urlpatterns += [
    path('login/', auth_views.LoginView.as_view()),
    path('logout/', auth_views.LogoutView.as_view()),
]

LOGIN_REDIRECT_URL = '/'
LOGOUT_REDIRECT_URL = '/login/'

Module 9 – Class Based Views


from django.views.generic import ListView
from .models import Post

class PostListView(ListView):
    model = Post
    paginate_by = 5

Module 10 – Deployment Basics


DEBUG = False
ALLOWED_HOSTS = ['yourdomain.com']

pip install gunicorn
python manage.py collectstatic

Django Module #4 – Templates & Static

Django Module 4 – Templates & Static

Module 4 – Templates & Static Files

Template Structure


blog/
 └── templates/
     └── blog/
         └── home.html

home.html


Welcome to Django

This page is rendered using templates

views.py


from django.shortcuts import render

def home(request):
    return render(request, 'blog/home.html')

Django Module #3 – URLs & Views

Django Module 3 – URLs & Views

๐Ÿ“˜ Django Tutorial – Module 3

Topic: URL Routing & Views


๐ŸŽฏ Learning Objectives

  • Understand Django URL routing
  • Create function-based views
  • Map URLs to views
  • Test URLs in browser

๐Ÿ”น What is a View?

A view is a Python function that receives a request and returns a response (HTML, text, JSON, etc.).

๐Ÿ”น Step 1: Create a View


# blog/views.py
from django.http import HttpResponse

def home(request):
    return HttpResponse("Hello Django!")
  

๐Ÿ”น Step 2: Create App-Level URLs


# blog/urls.py
from django.urls import path
from . import views

urlpatterns = [
    path('', views.home, name='home'),
]
  

๐Ÿ”น Step 3: Include App URLs in Project


# mysite/urls.py
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('blog.urls')),
]
  

๐Ÿ”น Step 4: Run Server


python manage.py runserver
  

๐ŸŒ Output

Open browser and visit:


http://127.0.0.1:8000/
  

๐Ÿงช Student Practice

  • Create another view about()
  • Map it to /about
  • Display your name and department

❓ Viva / Exam Questions

  1. What is URL routing in Django?
  2. What is the role of urls.py?
  3. Difference between view and template?
๐ŸŽ“ Teaching Tips:Ask students to trace URL → View → Response
Modify response text live
Show 404 error when URL is missing

Django Module 2 — Project & App Structure

Django Module 2 – Project & App Structure

๐Ÿ“˜ Django Tutorial – Module 2

Topic: Django Project & App Structure


๐ŸŽฏ Learning Objectives

  • Understand Django project vs app
  • Create a Django app
  • Explore Django folder structure
  • Register app in settings

๐Ÿ”น What is a Django Project?

A Django project is the entire web application. It contains settings, URLs, and configuration.

๐Ÿ”น What is a Django App?

A Django app is a module that performs a specific function (e.g., blog, accounts, attendance).

๐Ÿ”น Step 1: Create Django App


python manage.py startapp blog
  

๐Ÿ”น Step 2: Project Structure


mysite/
│
├── blog/
│   ├── admin.py
│   ├── apps.py
│   ├── models.py
│   ├── tests.py
│   └── views.py
│
├── mysite/
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
│
└── manage.py
  

๐Ÿ”น Step 3: Register App in settings.py


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

๐Ÿ”น Step 4: Run Server Again


python manage.py runserver
  

๐Ÿงช Student Practice

  • Create another app named pages
  • Add it to INSTALLED_APPS
  • Observe the folder structure

❓ Viva / Exam Questions

  1. What is the difference between Django project and app?
  2. Why do we add apps to INSTALLED_APPS?
  3. What is the role of manage.py?
๐ŸŽ“ Classroom Tip Keep Module 1 & Module 2 HTML files open Let students copy commands directly Ask them to explain project structure verbally

Django Module #1 – Introduction & Setup (HTML with Copy Button)

Django Module 1 – Introduction & Setup

๐Ÿ“˜ Django Tutorial – Module 1

Topic: Introduction & Environment Setup


๐ŸŽฏ Learning Objectives

  • Understand what Django is
  • Install Django
  • Create virtual environment
  • Run first Django server

๐Ÿ”น Step 1: Create Virtual Environment


python -m venv venv
venv\Scripts\activate
  

๐Ÿ”น Step 2: Install Django


pip install django
  

๐Ÿ”น Step 3: Verify Django Installation


django-admin --version
  

๐Ÿ”น Step 4: Create Django Project


django-admin startproject mysite
cd mysite
python manage.py runserver
  

๐ŸŒ Output

Open browser and visit:


http://127.0.0.1:8000/
  

๐Ÿงช Student Practice

  • Change server port to 9000
  • Stop and restart the server

❓ Viva Questions

  1. What is Django?
  2. Why do we use virtual environment?
  3. What is the purpose of manage.py?

#0 Intro & Setup

 

Django Tutorial in VS Code — 10 Modules (Hands‑On)

A step‑by‑step Django tutorial designed for teaching and self‑learning using Visual Studio Code. This blog is structured as 10 modules, each intended for ~1 hour of hands‑on practice, with clear explanations and runnable code.


Prerequisites

  • Python 3.10+

  • VS Code

  • VS Code Extensions:

    • Python (Microsoft)

    • Pylance

  • Basic Python knowledge


Module Overview

  1. Introduction to Django & Environment Setup

  2. Creating First Django Project & App

  3. URL Routing & Views

  4. Templates & Static Files

  5. Models & Database (ORM)

  6. Django Admin & Superuser

  7. Forms & CRUD Operations

  8. Authentication (Login / Logout / Signup)

  9. Class‑Based Views & Pagination

  10. Deployment Basics & Best Practices


Django #7a Project Sample

Employee Management System – Django (HRMS) Employee Management System (Django + MySQL) Roles: Admin, HR, Employee 1....