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
Views in Django handle HTTP requests and return HTTP responses. For our blog, we need a view to display all blog posts and another to display a single post. Open blog/views.py and add the following code:
from django.shortcuts import render
from .models import BlogPost
def blog_post_list(request):
posts = BlogPost.objects.all()
return render(request, 'blog/post_list.html', {'posts': posts})
def blog_post_detail(request, pk):
post = BlogPost.objects.get(pk=pk)
return render(request, 'blog/post_detail.html', {'post': post})
Step 6: Create Templates
Create a new directory named templates inside your blog app directory, and inside templates, create another directory named blog. Then, create two HTML files: post_list.html and post_detail.html. For post_list.html:
<h1>Blog Posts</h1>
<ul>
{% for post in posts %}
<li><a href='{% url 'post_detail' post.pk %}'>{{ post.title }}</a></li>
{% endfor %}
</ul>
And for post_detail.html:
<h1>{{ post.title }}</h1>
<p>{{ post.content }}</p>
Step 7: Add URL Patterns
Finally, you need to add URL patterns for your views. Open blog/urls.py (create this file if it doesn't exist) and add the following code:
from django.urls import path
from . import views
detail_pattern = '[0-9]+'
urlpatterns = [
path('', views.blog_post_list, name='post_list'),
path('<pk>/', views.blog_post_detail, name='post_detail'),
]
Then, include these URL patterns in your project's main urls.py file (blog_project/urls.py):
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('admin/', admin.site.urls),
path('blog/', include('blog.urls')),
]
Conclusion
You have now successfully created a simple blog using Django. You can run the development server using python manage.py runserver and access your blog at http://localhost:8000/blog/.
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.