Open In App

Building Blog CMS (Content Management System) with Django

Last Updated : 07 Jun, 2023
Like Article

Django is a python based web application framework that is helpful for building a variety of web applications. Django also includes an extensible Django-Admin interface, Default SQLIte3 database, which is also extensible to PostgreSQL, MySQL databases, and a few other components to build efficient web apps.

Install and Setup Django

Create a directory for our blog, install and activate the virtual environment. Then install Django using the below commands

# creating directory for our project
mkdir gfgblog && cd gfgblog

# installing virtual environment
pip install virtualenv
python3 -m venv env

# activating virtual environment
source env/bin/activate

# installing django
pip install django

As now we have installed Django we will now create a Django project which will set up a basic Django app

django-admin startproject gfgblog
cd gfgblog

In our gfgblog Django app, we will have these files

  •    – empty file
  •   – for routing our Django project
  •   – has all settings for our Django project
  •, wsgi – helpful while deploying our app

We are in the Django web app directory. Now we will make some migrations for our database which will be SQLite3 which will set up some default tables to run our app in our database. Then we will create a superuser for our app.

# migrating tables
python3 makemigrations
python3 migrate

# create and enter the details for superuser
python3 createsuperuser

creating, migrating django-app

Now run the Django development server and open port 8000 in your localhost

# running python development server
python3 runserver

default django page

Now stop the server and go into gfgblog directory in our gfgblog Django app directory and open file using your code editor. You can look at the tree of our gfgblog directory from the below picture

django app directory tree structure

Create a Blog App in Django –

Now we will create our actual blog app and database for it. Go to the gfgblog project directory. You can see our SQLite3 database, gfgblog Django app. now in this directory create a new app named blog. The below command will create a new app for us.

# creating an app named blog
python3 startapp blog

The new app directory has 5 default files

  • –  an empty file
  • –  for managing admin interface
  • –  for managing app configuration
  • –  for managing database models of the app
  • –  for testing the app
  • –  for managing behavior and logic of the app

As we have created the app now we have to tell the django app that there is a new app in our project. For that go to in gfgblog directory and open file. Go to installed apps section of the settings file and add the name of the app we created; in our case its blog




Creating Models for Blog CMS – 

Now we will create Django models for the app. You can learn more about Django Models from this tutorial – Django Models

To create Django Models open in your editor and add the below code


# importing django models and users
from django.db import models
from django.contrib.auth.models import User
    (2, "Delete")
# creating an django model class
class posts(models.Model):
    # title field using charfield constraint with unique constraint
    title = models.CharField(max_length=200, unique=True)
    # slug field auto populated using title with unique constraint
    slug = models.SlugField(max_length=200, unique=True)
    # author field populated using users database
    author = models.ForeignKey(User, on_delete= models.CASCADE)
    # and date time fields automatically populated using system time
    updated_on = models.DateTimeField(auto_now= True)
    created_on = models.DateTimeField()
    # content field to store our post
    content = models.TextField()
    # meta description for SEO benefits
    metades = models.CharField(max_length=300, default="new post")
    # status of post
    status = models.IntegerField(choices=STATUS, default=0)
    # meta for the class
    class Meta:
        ordering = ['-created_on']
    # used while managing models from terminal
    def __str__(self):
        return self.title

Save the file and make migration so that the fields will be created in our database using the below commands

# creates migrations for the blog app
python3 makemigrations blog

# migrates the blog app
python3 migrate blog

So far we are good with creating models and setting our database for storing our posts, but we should present them to users. Luckily Django comes with its own templating language with which we can build dynamic HTML pages to serve our users. We can also add styling using CSS and JavaScript. You can learn more about Django templates in this tutorial – Django Templates

Creating Templates for Blog CMS –

To create templates first create a template directory in the blog app (You can also create it outside the blog app but in this way, it would be easy to manage). To manage templates in an easy way for each app in the project we have to do some changes in the settings. Go to the project settings file and replace template settings with the code below


TEMPLATES_DIR = os.path.join(BASE_DIR,'templates')
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [TEMPLATES_DIR],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [

Now we will create templates for our blog. As Django can build pages dynamically we will build our templates according to it. Now we will create a base template that serves as a base for all the pages in our blog.


<!DOCTYPE html>
<html lang="en">
  <!-- will be replaced with meta content -->
  {% block metatags %}{% endblock %}
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <link rel="stylesheet" href="" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">
html, body {
  height: 100%;
  body {
    font-family: sans-serif;
    font-size: 18px;
    background-color: #fdfdfd;
display: flex;
  flex-direction: column;
  #nofooter {
flex: 1 0 auto;
darkbtn {
color: #66ff99; }
 .dark-mode {
    background-color: black;
    color: white;
  #foot {
    flex-shrink: 0;
    font-family: sans-serif;
background-color: #BDFFD3; color: #00308F;
    bottom: 0px;
    width: 100%;
.card-group,.card,.card-footer,.card-body {
border: none;
<body><div id="bodydiv"><div id="nofooter">
  <nav class="navbar navbar-expand-lg navbar-light bg-light shadow" id="topnav">
    <div class="container-fluid">
      <a class="navbar-brand" href="{% url 'home' %}">
      <img src="/static/icons/logo.png" width="30" height="30" class="d-inline-block rounded align-top" alt="logo" loading="lazy">
      <div class="nav justify-content-end">
        <div class="d-inline"><a class="nav-link text-black font-weight-bold" href="{% url 'posts'%}">Posts</a></div>
   <!-- will be replaced with post content -->
{% block content %}
{% endblock content %}
<footer class="footer" id="foot">
<div class="container"><h5 style="text-align:center;">Enjoy the Blog and Stay Updated</h5>
<nav class="navbar navbar-expand-lg">
<div class="container-fluid">
<p>Copyright © GeeksForGeeks</p>
<div class="nav justify-content-center" id="navfooter">
<span><a class="nav-link text-black font-weight-bold" href="{% url 'about' %}">About</a></span>
<span><a class="nav-link text-black font-weight-bold" href="{% url 'privacy' %}">Privacy</a></span>
<span><a class="nav-link text-black font-weight-bold" href="{% url 'tos' %}">Terms</a></span>
<span><a class="nav-link text-black font-weight-bold" href="#">Feed</a></span>

This will be our base template, You can block like {% block name %}{% endblock %} which will be replaced with the content assigned to them, Now we will create a home template that would be the homepage for our blog which will have the latest posts.


{% extends "base.html" %}
{% block metatags %}
<title>Home | GeeksForGeeks</title>
<meta name="description" content="A destination for Learning">
<meta property="og:title" content="GeeksForGeeks">
<meta property="og:site_name" content="GeeksForGeeks">
<meta property="og:url" content="">
<meta property="og:description" content="A destination for Learning">
<meta property="og:type" content="website">
{% endblock %}
{% block content %}
<style type="text/css">
    body {
       font-family:  'Raleway', sans-serif;}
    .head_text {
        color: white;
.h1, h4 {
font-family: 'Raleway', sans-serif;
#mainhome {
text-align: center; }
<header class="jumbotron" style="background-color: #BDFFD3; color: #00308F;">
    <div class="container">
      <p class="h1"> Welcome to <strong>GeeksForGeeks</strong></p>
<div class="container">
    <div class="row">
        <div class="col-md-8 mt-3 left">
            {% for post in posts_list %}
            <div class="shadow-none card mb-4" id="newsfeed">
                <div class="card-body">
                    <h3 class="card-title">{{ post.title }}</h3>
                    <p class="card-text text-muted h6"><span>{{ }} {{ }}</span> | {{ post.created_on}} </p>
                    <p class="card-text">{{post.metades }}</p>
                    <span><a href="{% url 'post_detail' post.slug  %}" class="btn btn-outline-primary">Read More →</a></span><span><a href="{% url 'post_detail_amp' post.slug %}" class="btn btn-outline-success">AMP page</a></span>
            {% endfor %}
{% if is_paginated %}
  <nav aria-label="Page navigation container"></nav>
  <ul class="pagination justify-content-center">
    {% if page_obj.has_previous %}
    <li><a href="?page={{ page_obj.previous_page_number }}" class="btn btn-outline-info" style="font-family: sans-serif;">« PREV </a></li>
    {% endif %}
    {% if page_obj.has_next %}
    <li><a href="?page={{ page_obj.next_page_number }}" class="btn btn-outline-info" style="font-family: sans-serif;"> NEXT »</a></li>
    {% endif %}
{% endif %}

We have created our homepage with pagination. Posts will be displayed using the for a loop. Now we will add our last template to display posts individually. 


{% extends 'base.html' %}
{% block metatags %}
<title>{{ object.title }}</title>
<meta name="description" content="{{ object.metades}}" />
<meta property="og:title" content="{{ object.title }}">
<meta property="og:site_name" content="GeeksForGeeks">
<meta property="og:url" content="{% url 'post_detail' object.slug %}">
<meta property="og:description" content="{{ object.metades }}">
<meta property="og:type" content="article">
{% endblock %}
{% block content %}
<style type="text/css">
#container img {
border-radius: 29px;
width: 100%;
height: 360px;
opacity: 0.7;
align-content: center;
#container img {
opacity: 1.0; }
a {text-align: center; text-decoration: none;}
<script type="application/ld+json">
  "@context": "",
  "@type": "Post",
  "headline": "{{ object.title }}",
  "description": "{{ object.metades }}",
  "mainEntityOfPage": {
    "@type": "WebPage",
    "@id": "{% url 'post_detail' object.slug %}"
  "author": {
    "@type": "Person",
    "name": "{{ }} {{ "
  "publisher": {
    "@type": "Organization",
    "name": "GeeksForGeeks",
  "datePublished": "{{ news.created_on }}",
  "dateModified": "{{ news.created_on }}",
  "mentions": "{{ object.source }}"
<div class="container">
  <div class="row"
<div class="col-md-6 left">
        <h1 class="card-title">{% block title %} {{ object.title }} {% endblock title %}</h1>
        <p class=" text-muted">{{ }} {{ }} |{{ object.created_on }}</p>
        <p class="card-text">{{ object.content | safe }}</p>
{% endblock content %}

Both homepage and post page will be built by extending the base template and replacing blocks with the data stored in our database. Both also have a Facebook open graphs for better sharing and social network and the post page has a structured schema for better SEO additionally. We will build AMP template incoming tutorial.

Creating Views for Blog CMS – 

Now open file in the blog directory. This file has the logic to run the blog app. We will be using class-based views for the blog app. Class Based Generic Views are an advanced set of Built-in views which are used for the implementation of selective view strategies such as Create, Retrieve, Update, Delete. 


# importing models and libraries
from django.shortcuts import render
from .models import posts
from django.views import generic
from django.views.decorators.http import require_GET
from django.http import HttpResponse
# class based views for posts
class postslist(generic.ListView):
    queryset = posts.objects.filter(status=1).order_by('-created_on')
    template_name = 'home.html'
    paginate_by = 4
# class based view for each post
class postdetail(generic.DetailView):
    model = posts
    template_name = "post.html"


Creating Routes for Blog CMS – 

For Routing our blog app just go into the blog app directory and create a file to route our app. see the code below 


# importing django routing libraries
from . import views
from django.urls import path, include
from .views import *
from .feeds import blogFeed
urlpatterns = [
    # home page
    path('', views.postslist.as_view(), name='posts'),
    # route for posts
    path('<slug:slug>/', views.postdetail.as_view(), name='post_detail'),

 Go to file in gfgblog directory and route our blog app. See the code below for reference


from django.contrib import admin
from django.urls import path, include, re_path
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
    # urls handling admin route
    # urls handling blog routes
    path('', include('blog.urls')),

Done, Let’s Now run the server using 

Python runserver

Blog CMS Screenshots –

HomePage –  

homepage with latest’s posts

A sample post page – 

sample post page

Posts management with Django Admin

posts management

User management via Django Admin

User management

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads