Building a Blog Application with Django Skip to main content

Building a Blog Application with Django


In this tutorial, we will build a Blog application with Django that allows users to create, edit, and delete posts. The homepage will list all blog posts, and there will be a dedicated detail page for each individual post. Django is capable of making more advanced stuff, but making a blog is an excellent first step to get a good grasp over the framework.

 Pre-Requirements

Django is an open-source web framework, written in Python, that follows the model-view-template architectural pattern. So Python is needed to be installed on your machine. Unfortunately, there was a significant update to Python several years ago that created a big split between Python versions, namely Python 2 the legacy version and Python 3 the version in active development. Since Python 3 is the current version in active development and addressed as the future of Python, Django rolled out a significant update, and now all the releases after Django 2.0 are only compatible with Python 3.x. Therefore this tutorial is strictly for Python 3.x. Make sure you have Python 3 installed on your machine if not follow the below guides.
  •  Windows Users
1. Download the latest version of Python 3 from the official website: [https://www.python.org/downloads/windows/](https://www.python.org/downloads/windows/).
2. Run the installer.
3. Check the box that says "Add Python 3.x to PATH".
4. Click "Install Now".
5. Wait for the installation to complete.
  •  Mac and Unix Users
1. Open the terminal.
2. Type `python3 --version` to check if Python 3 is already installed.
3. If Python 3 is not installed, download the latest version of Python 3 from the official website: [https://www.python.org/downloads/mac-osx/](https://www.python.org/downloads/mac-osx/).
4. Run the installer.
5. Wait for the installation to complete.

Creating and Activating a Virtual Environment

While building python projects, it's a good practice to work in virtual environments to keep your project and its dependency isolated on your machine. Here is how you can create and activate a virtual environment:
  • Windows Users
1. Open the command prompt.
2. Navigate to the directory where you want to create the virtual environment.
3. Type `virtualenv django`.
4. Navigate to the newly created directory by typing `cd django`.
5. Type `Scripts\activate.bat`.
  • Mac and Unix Users
1. Open the terminal.
2. Navigate to the directory where you want to create the virtual environment.
3. Type `python3 -m venv django`.
4. Navigate to the newly created directory by typing `cd django`.
5. Type `source bin/activate`.
Now you should see `(django)` prefixed in your terminal, which indicates that the virtual environment is successfully activated. If not, then go through the guide again.

Installing Django in the Virtual Environment

To install Django on your virtual environment, run the following command:
pip install Django

This will install the latest version of Django in our virtual environment.

Setting up the Project

1. Create a new Django project by running the following command:
django-admin startproject myblog
2. Navigate to the newly created directory by typing `cd myblog`.
3. Create a new Django app by running the following command:
python manage.py startapp blog

4. Open the `settings.py` file located in the `myblog` directory.

5. Add `'blog'` to the `INSTALLED_APPS` list.
6. Add the following lines at the end of the file:
 MEDIA_URL = '/media/'

 MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
7. Open the `urls.py` file located in the `myblog` directory.
8. Add the following lines at the end of the file:
from django.conf.urls.static import static

from django.conf import settings

urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
9. Open the `urls.py` file located in the `blog` directory.
10. Replace the contents of the file with the following code:
 from django.urls import path

    from .views import (

        BlogListView,

        BlogDetailView,

        BlogCreateView,

        BlogUpdateView,

        BlogDeleteView,

    )

    urlpatterns = [

        path('', BlogListView.as_view(), name='home'),

        path('post/<int:pk>/', BlogDetailView.as_view(), name='post_detail'),

        path('post/new/', BlogCreateView.as_view(), name='post_new'),

        path('post/<int:pk>/edit/', BlogUpdateView


To see the blog site, you can run the following command in your terminal:
python manage.py runserver

This will start the development server at http://127.0.0.1:8000/. Open this URL in your browser to see the blog site.

To deploy the blog site, you can follow the instructions provided in the following resources:

I hope this helps!

Comments

You may like

Latest Posts

SwiGLU Activation Function

Position Embedding: A Detailed Explanation

How to create a 1D- CNN in TensorFlow

Introduction to CNNs with Attention Layers

Meta Pseudo Labels (MPL) Algorithm

Video Classification Using CNN and Transformer: Hybrid Model

Graph Attention Neural Networks