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 Your App and Run Migrations
You need to tell Django that your app is installed and apply any database migrations. Add 'blog' to INSTALLED_APPS in settings.py, then run:
python manage.py makemigrations
python manage.py migrate
Step 5: Create Views and Templates
Create a view for displaying blog posts and a template to render the posts. Open blog/views.py and add:
from django.shortcuts import render
from .models import BlogPost
def blog_index(request):
posts = BlogPost.objects.all()
return render(request, 'blog_index.html', {'posts': posts})
Create a new directory templates inside your blog app, and inside templates, create another directory named blog. Inside blog/templates/blog, create a file named blog_index.html with the following content:
{% extends 'base.html' %}
{% block content %}
<h1>Blog Index</h1>
<ul>
{% for post in posts %}
<li>{{ post.title }} ({{ post.created_at }})</li>
{% endfor %}
</ul>
{% endblock %}
And a basic base.html in the same directory:
<!DOCTYPE html>
<html>
<head>
<title>Blog</title>
</head>
<body>
<div>
{% block content %}{% endblock %}
</div>
</body>
</html>
Step 6: Add URL Patterns
Finally, you need to map URLs to your views. Open blog/urls.py (create it if it doesn't exist) and add:
from django.urls import path
from . import views
durlpatterns = [
path('', views.blog_index, name='blog_index'),
]
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')),
]
Conclusion
You've now created a simple blog with Django. You can run the development server with python manage.py runserver and access your blog at http://localhost:8000/blog/. This is a basic example to get you started with Django and blogging.
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.