Session Overview

  • Project Introduction
  • Create Blog Project
  • Configure MySQL Database
  • Create Blog App
  • Templates & Static Files
  • Create Base Template

Project Introduction

So far, we have learned the fundamental concepts of Django such as Models, Views, Templates, Forms and CRUD operations.

Now it is time to build a complete real-world application by applying all these concepts together.

Project Name
Django Blog Portal
Project Features
Module Description
Authentication Admin Login & Logout
Category Manage Blog Categories
Posts Create, Edit and Delete Blog Posts with Image
Comments User Comments
Search Search Blog Posts
Pagination Display Posts Page Wise
Goal By the end of this project, you will have a complete Django Blog Application that can be included in your portfolio.

Create Blog Project

We will create a new Django Project inside a Virtual Environment.

This keeps the project independent and manages all required packages in one place.

Step 1 - Create Project Folder
mkdir blogproject

cd blogproject
Step 2 - Create Virtual Environment
python -m venv venv
Step 3 - Activate Virtual Environment
venv\Scripts\activate
Step 4 - Install Django
pip install django
Step 5 - Create Django Project
django-admin startproject blogproject
Step 6 - Install Required Packages
pip install mysqlclient

pip install pillow

pip install django-ckeditor
Step 7 - Open Visual Studio Code
code .
Step 8 - Verify Installation
python manage.py runserver
Result: The Django development server starts successfully and the default welcome page is displayed in the browser.
Why are we installing these packages?
Package Purpose
Django Python Web Framework
mysqlclient Connect Django with MySQL Database
Pillow Support Image Upload in Blog Posts
django-ckeditor Rich Text Editor for Blog Content

Configure MySQL Database

By default, Django uses SQLite. For this project, we will use MySQL as our database.

Step 1 - Create Database
CREATE DATABASE BlogDB;
Step 2 - Open settings.py
blogproject/settings.py
Step 3 - Update DATABASES
DATABASES = {

    'default': {

        'ENGINE': 'django.db.backends.mysql',

        'NAME': 'BlogDB',

        'USER': 'root',

        'PASSWORD': '',

        'HOST': 'localhost',

        'PORT': '3306',

    }

}
Step 4 - Apply Migrations
python manage.py migrate
Result: Django creates all required system tables inside the MySQL database.
Why are we using MySQL?
  • Suitable for real-world applications.
  • Supports large amounts of data.
  • Most companies use relational databases like MySQL or PostgreSQL.
  • The Django ORM works the same with SQLite and MySQL.

Create Blog App

A Django Project can contain multiple Applications.

We will create a separate application named blog to manage all blog-related features.

Step 1 - Create App
python manage.py startapp blog
Step 2 - Register App

Open:

blogproject/settings.py

Add the app name inside INSTALLED_APPS.

'blog',
Why did we create a Blog App?

Django follows a modular architecture where each application is responsible for a specific feature.

The blog app will contain all files related to the Blog Module such as Models, Views, Forms and Templates.

Project Structure
blogproject
│
├── blog
│   ├── models.py
│   ├── views.py
│   ├── forms.py
│   ├── admin.py
│   └── migrations
│
├── blogproject
│
├── manage.py
│
└── venv

Configure Project Resources

Before developing the Blog Project, we will configure the Templates, Static Files and Media Files.

This prepares the project for storing HTML pages, CSS, JavaScript and uploaded Blog Images.

Step 1 - Create Folder Structure
blogproject
│
├── blog
│
├── templates
│
├── static
│   ├── css
│   ├── js
│   └── images
│
├── media
│
├── manage.py
│
└── blogproject
Step 2 - Configure Templates Folder

Open blogproject/settings.py and update the TEMPLATES setting.

'DIRS': [BASE_DIR / 'templates'],
Purpose: Django will now search for HTML files inside the templates folder.
Step 3 - Configure Static Files

Add the following code in settings.py.

STATIC_URL = 'static/'

STATICFILES_DIRS = [

    BASE_DIR / 'static',

]
Purpose: Static files include CSS, JavaScript and Images used by the website.
Step 4 - Configure Media Files
MEDIA_URL = '/media/'

MEDIA_ROOT = BASE_DIR / 'media'
Purpose: Uploaded files such as Blog Images will be stored inside the media folder.
Step 5 - Configure Media URLs

Open blogproject/urls.py.

Import the following modules.

from django.conf import settings

from django.conf.urls.static import static

Add the following code after urlpatterns.

if settings.DEBUG:

    urlpatterns += static(

        settings.MEDIA_URL,

        document_root=settings.MEDIA_ROOT

    )
Purpose: This allows Django to display uploaded media files while running the project in development mode.
Static Files vs Media Files
Static Files Media Files
CSS Uploaded Images
JavaScript User Uploaded Files
Website Logo Blog Featured Images
Result: The Blog Project is now fully configured for Templates, Static Files and Media Files. We can start developing the application without making any additional project-level configuration.

Create Base Template

Instead of creating the layout for every page separately, we will create a common base.html file.

This file contains the common Header, Navigation Bar and Footer used throughout the project.

File Location
templates/base.html
base.html
<!DOCTYPE html>

{% load static %}

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta name="viewport"
          content="width=device-width, initial-scale=1">

    <title>Django Blog Portal</title>

    <!-- Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.7/dist/css/bootstrap.min.css"
          rel="stylesheet">

    <!-- Font Awesome -->
    <link rel="stylesheet"
          href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.7.2/css/all.min.css">

    <!-- Custom CSS -->
    <link rel="stylesheet"
          href="{% static 'css/style.css' %}">

</head>

<body>

<nav class="navbar navbar-expand-lg navbar-dark bg-dark">

    <div class="container">

        <a class="navbar-brand"
           href="/">

            <i class="fa-solid fa-blog"></i>

            Django Blog

        </a>

        <button class="navbar-toggler"
                data-bs-toggle="collapse"
                data-bs-target="#menu">

            <span class="navbar-toggler-icon"></span>

        </button>

        <div class="collapse navbar-collapse"
             id="menu">

            <ul class="navbar-nav ms-auto">

                <li class="nav-item">

                    <a class="nav-link"
                       href="/">

                        <i class="fa-solid fa-house"></i>

                        Home

                    </a>

                </li>

                <li class="nav-item">

                    <a class="nav-link"
                       href="#">

                        <i class="fa-solid fa-folder"></i>

                        Categories

                    </a>

                </li>

                <li class="nav-item">

                    <a class="nav-link"
                       href="#">

                        <i class="fa-solid fa-file-lines"></i>

                        Posts

                    </a>

                </li>

                <li class="nav-item">

                    <a class="nav-link"
                       href="#">

                        <i class="fa-solid fa-right-to-bracket"></i>

                        Login

                    </a>

                </li>

            </ul>

        </div>

    </div>

</nav>

<div class="container my-5">

    {% block content %}

    {% endblock %}

</div>

<footer class="bg-dark text-white text-center py-3">

    <p class="mb-0">

        © 2026 Django Blog Portal |
        Developed During Python Django Training

    </p>

</footer>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.7/dist/js/bootstrap.bundle.min.js"></script>

</body>

</html>

Configure URLs and Run Project

Finally, we will connect the Blog App with the main project and display the Home page.

Step 1 - Create urls.py inside Blog App
blog/urls.py
from django.urls import path

from . import views

urlpatterns = [

    path('', views.home, name='home'),

]
Step 2 - Update blog/views.py
from django.shortcuts import render

def home(request):

    return render(request,'home.html')
Step 3 - Include App URLs

Open blogproject/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 - Create Home Page
templates/home.html
{% extends 'base.html' %}

{% block content %}

<div class="text-center mt-5">

    <h1>Welcome To Django Blog Portal</h1>

    <p>

        Our Blog Project setup is completed successfully.

    </p>

</div>

{% endblock %}
Step 5 - Run Project
python manage.py runserver
Result: The Home page is displayed using the common base.html layout with the Navigation Bar and Footer.

Quick Revision

Project Setup
  • Virtual Environment
  • Django Project
  • Required Packages
MySQL
  • Database Configuration
  • settings.py
  • Migration
Blog App
  • startapp
  • INSTALLED_APPS
  • Project Structure
Templates
  • templates
  • static
  • media
Layout
  • Bootstrap
  • Navbar
  • Footer
Packages
  • mysqlclient
  • Pillow
  • CKEditor
Commands Practiced Today
python -m venv venv

pip install django

django-admin startproject blogproject

python manage.py startapp blog

python manage.py migrate

python manage.py runserver

Assignment

Build a News Portal Project

Create a new Django project named News Portal by following the same steps demonstrated in today's Blog Project.

  1. Create a new Django Project and Virtual Environment.
  2. Configure MySQL Database.
  3. Create a Django App named news.
  4. Configure the templates, static and media folders.
  5. Create a professional layout using Bootstrap with:
    • Responsive Navigation Bar
    • Footer
    • Custom CSS
  6. Create a Home Page using Template Inheritance.
Note: Throughout the remaining sessions, continue developing the News Portal by implementing the same features demonstrated in the Blog Project.