Building a Simple Blog with Django
Learn how to create a basic blog using Django, a high-level Python web framework, in this step-by-step tutorial.
Introduction to Django Blog
Django is a powerful Python web framework that allows you to build robust and scalable web applications. In this tutorial, we will create a simple blog using Django.
Step 1: Install Django and Create a New Project
To start, you need to have Python and pip installed on your system. Then, you can install Django using pip:
pip install django
After installing Django, create a new project using the following command:
django-admin startproject blog_project
Step 2: Create a New App
Navigate into your project directory and create a new app for your blog:
cd blog_project
python manage.py startapp blog
Step 3: Define Your Models
In Django, models represent data stored in your database. For our blog, we need a model for blog posts. Open blog/models.py and add the following code:
from django.db import models
class BlogPost(models.Model):
title = models.CharField(max_length=255)
content = models.TextField()
created_at = models.DateTimeField(auto_now_add=True)
Step 4: Activate the App and Run Migrations
To use the app and its models, you need to add it to INSTALLED_APPS in your project's settings file (blog_project/settings.py) and then run migrations:
INSTALLED_APPS = [
# ...
'blog.apps.BlogConfig',
]
Then, run the following commands in your terminal:
python manage.py makemigrations
python manage.py migrate
Step 5: Create Views and Templates
Create a view to handle requests to your blog and a template to display blog posts. Open blog/views.py and add:
from django.shortcuts import render
from .models import BlogPost
def blog(request):
posts = BlogPost.objects.all()
return render(request, 'blog.html', {'posts': posts})
Create a new directory named templates inside your app directory, and inside templates, create another directory named blog. Inside blog/templates/blog, create a file named blog.html and add your HTML template there.
Step 6: Add URL Patterns
Finally, you need to map URLs to your views. Open blog/urls.py (create this file if it doesn't exist) and add:
from django.urls import path
from . import views
durlpatterns = [
path('', views.blog, name='blog'),
]
Then, include this URL configuration in your main project's urls.py:
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('admin/', admin.site.urls),
path('blog/', include('blog.urls')),
]
Now, you can run your development server and access your blog at http://localhost:8000/blog/:
python manage.py runserver
Ready for more? These paid resources pick up where this lesson leaves off.
Project-based Django video courses — the perfect paid next step after these free lessons.
Browse on Udemy →Hand-picked Django books to master the fundamentals offline.
See on Amazon →Some links on this page are affiliate links: we may earn a commission at no extra cost to you. We only recommend tools we believe are genuinely useful.