Blogging Platform using Django
Last Updated :
20 May, 2025
Our task is to build a simple blogging platform using Django. We will learn how to create, display, edit, and delete blog posts with images using Django’s models, views, templates, and admin panel. Step-by-step, we’ll set up the project, connect it to a database, and run it locally.
Project Setup
Prerequisites:
Start by creating your Django project and app:
django-admin startproject blogsite
cd blogsite
python manage.py startapp gallery
Create the Product Model
The Product model represents a blog post with an image and timestamps.
gallery/models.py:
Python
from django.db import models
class Product(models.Model):
name = models.CharField(max_length=255)
description = models.TextField()
image = models.ImageField(upload_to='products/')
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
def __str__(self):
return self.name
def edit(self, name, description, image):
self.name = name
self.description = description
self.image = image
self.save()
def short_description(self):
# Split the description into words
words = self.description.split()
if len(words) > 50:
# Join the first 50 words and add "..." at the end
return ' '.join(words[:30]) + '...'
else:
# If the description is already less than 50 words, return it as is
return self.description
Register Model in Admin
gallery/admin.py:
Python
from django.contrib import admin
from .models import Product
# Register your models here.
admin.site.register(Product)
gallery/forms.py:
Python
from django import forms
from .models import Product
class ProductForm(forms.ModelForm):
class Meta:
model = Product
fields = ['name', 'description', 'image']
Define Views
gallery/views.py:
Python
from django.shortcuts import render
from .models import Product
from django.http import HttpResponse
from django.shortcuts import render, redirect, get_object_or_404
from .models import Product
from .forms import ProductForm
def product_list(request):
products = Product.objects.all()
return render(request, 'myapp/index.html', {'products': products})
def product_detail(request, pk):
product = Product.objects.get(pk=pk)
return render(request, 'myapp/index2.html', {'product': product})
def edit_product(request, pk):
product = get_object_or_404(Product, pk=pk)
if request.method == 'POST':
form = ProductForm(request.POST, instance=product)
if form.is_valid():
form.save()
return redirect('product_list')
else:
form = ProductForm(instance=product)
return render(request, 'myapp/edit.html', {'form': form})
def delete_product(request, pk):
product = get_object_or_404(Product, pk=pk)
if request.method == 'POST':
product.delete()
return redirect('product_list')
return render(request, 'myapp/delete.html', {'product': product})
def home(request):
return HttpResponse('Hello, World!')
URL Configuration
gallery/urls.py:
Python
from django.urls import path
from . import views
urlpatterns = [
path('home/', views.home, name='home'),
path('', views.product_list, name='product_list'),
path('<int:pk>/', views.product_detail, name='product_detail'),
path('<int:pk>/edit/', views.edit_product, name='edit_product'),
path('<int:pk>/delete/', views.delete_product, name='delete_product'),
]
blogsite/urls.py:
Python
from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('gallery.urls')),
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
In blogsite/settings.py, add:
Python
import os
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
HTML Templates
Create a templates/myapp/ directory inside the gallery app and add the following:
edit.html: HTML file to edit the Blog.
HTML
<h2>Edit Blog Post</h2>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Save Changes</button>
</form>
delete.html: HTML file to delete the Blog.
HTML
<h2>Delete Blog Post</h2>
<p>Are you sure you want to delete "{{ product.name }}"?</p>
<form method="post">
{% csrf_token %}
<button type="submit">Yes, Delete</button>
</form>
index.html: HTML file to show all the list of the Blog.
Python
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Blog Website</title>
<!-- Add Bootstrap CSS link here -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
</head>
<body>
<div class="container mt-5">
<h1>Today's New Blogs</h1>
<div class="row">
{% for product in products %}
<div class="col-md-4 mb-4">
<div class="card">
<a href="{% url 'product_detail' product.pk %}">
<img src="{{ product.image.url }}" alt="{{ product.created_name }}" class="card-img-top">
</a>
<div class="card-body">
<h5 class="card-title">{{ product.name }}</h5>
<p class="card-text">{{ product.short_description }}</p>
<a href="{% url 'edit_product' product.pk %}">Edit</a>
<a href="{% url 'delete_product' product.pk %}">Delete</a> <br>
<small class="card-text">Created at: {{ product.created_at }}</small> <br>
<small class="card-text">Updated at: {{ product.updated_at }}</small>
</div>
</div>
</div>
{% endfor %}
</div>
</div>
<!-- Add Bootstrap JS and jQuery scripts here (if needed) -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
</body>
</html>
index2.html: HTML file to show the full details of the Blog.
Python
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{{ product.name }} - Blog Detail</title>
</head>
<body>
<h1>{{ product.name }} - Blog </h1>
<div>
<img src="{{ product.image.url }}" alt="{{ product.name }}" width="200">
</div>
<h2>{{ product.name }}</h2>
<p>{{ product.description }}</p>
<a href="{% url 'edit_product' product.pk %}">Edit</a>
<a href="{% url 'delete_product' product.pk %}">Delete</a> <br>
<a href="{% url 'product_list' %}">Back to Blogs Home Page</a>
</body>
</html>
Install Required Packages
Make sure to install Pillow for image handling:
pip install Pillow
Database Setup and Superuser Creation
Apply Migrations
python manage.py makemigrations
python manage.py migrate
Create a Superuser
python manage.py createsuperuser
You will be prompted to enter a username, email, and password.
Once done, run:
python manage.py runserver
Visit: http://127.0.0.1:8000/admin/
Login using the superuser credentials and add blog entries with images via the admin interface.

Output:
.png)



Similar Reads
Blogging Platform using Next JS
In this project, we will explore the process of building The Blogging Platform with Next.js. Blogging Platform is a web application that allows users to create and publish blog posts. The platform provides a user-friendly interface for managing blog content and includes functionalities to create new
5 min read
Blogging Platform using MERN Stack
The blogging platform is developed using the MERN (MongoDB, ExpressJS, ReactJS, NodeJS) stack, allowing users to create, read, update, and delete blog posts. It provides a user-friendly interface for managing blog content, including features like adding new posts and viewing existing posts. Output P
5 min read
E-book Library using Django
In this article, we will demonstrate how to build an E-book Library using Django. We have incorporated the functionality of login and registration, ensuring that users must first log in and register to access and read books. Additionally, after logging in, users gain the capability to both read and
15+ min read
Blog Post Recommendation using Django
In this article, we will guide you through the creation of a blog post recommendation system using Django. Our article covers the integration of a user-friendly login system with a registration form, ensuring a seamless experience for your website visitors. Additionally, we have implemented a sophis
10 min read
Python Compiler Using Django
In this article, we will explore the creation of a Python compiler using Django. Users can input their Python code into the designated code area, and upon clicking the "Run" button, the compiler will generate the corresponding output. What is Python Compiler?A Python compiler is a program or tool th
4 min read
Job Board using Django
In this article, we will guide you through creating a Job Board using Django in Python. Job Board Using DjangoBelow, is the step-by-step Implementation of a language learning app using Django in Python: Starting the Project FolderTo start the project use this command django-admin startproject Job_Bo
3 min read
Top 4 Blogging Platforms That You Can Consider
A blog is an informational website written in an informal styled text, present on the internet. It can consist of information about various broad or specific topics like technology, photography, news, reviews, and much more. In today's world, Blogging has become one of the most high-paying career op
5 min read
Building APIs using FastAPI with Django
Combining FastAPI and Django can leverage the strengths of both frameworks: FastAPI's high performance for building APIs and Django's powerful ORM and admin interface. In this guide, we'll outline how to integrate FastAPI into a Django project to build high-performance APIs.In this article, we will
2 min read
Django - Dealing with warnings
Django is a powerful web framework that provides a clean, reusable architecture to build robust applications quickly. It embraces the DRY (Don't Repeat Yourself) principle, allowing developers to write minimal, efficient code.Create and setup a Django project:Prerequisite: Django - Creating projectA
2 min read
Online Resume Builder using Django
In this article, we will create an online resume builder web app. We will start by filling out a form with our personal information. Once the form is completed, we can generate a resume. Additionally, we will ensure the PDF generation quality to make the final result closely resemble the original re
9 min read