0% found this document useful (0 votes)
2 views

Create a Simple Blog Application Using Django - DEV Community

This document provides a step-by-step guide to creating a simple blog application using Django, targeting beginners with basic Python and Django knowledge. It covers project setup, model definition, view creation, URL configuration, and template design, culminating in running the development server to view the application. The guide emphasizes understanding Django's structure and prepares readers for more advanced topics in future posts.

Uploaded by

piyush
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Create a Simple Blog Application Using Django - DEV Community

This document provides a step-by-step guide to creating a simple blog application using Django, targeting beginners with basic Python and Django knowledge. It covers project setup, model definition, view creation, URL configuration, and template design, culminating in running the development server to view the application. The guide emphasizes understanding Django's structure and prepares readers for more advanced topics in future posts.

Uploaded by

piyush
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

2/24/25, 11:29 AM Create a Simple Blog Application Using Django - DEV Community

kihuni
Posted on Jul 26, 2024

Create a Simple Blog Application Using


Django
#django #webdev #backend #beginners

In our last blog post, we covered the basic concepts of Django. Now, let's create a
simple blog application to solidify our understanding.

Requirements:
Basic Python knowledge
Fundamental understanding of Django
Basic proficiency in using the terminal

To create a simple blog, we will follow these steps:

Step 1: Create a project folder and Install the Virtual


Environment
First, create a folder called Simple-blog , then inside the folder, install the virtual
environment and activate it.

https://dev.to/kihuni/create-a-simple-blog-application-using-django-4fo9 1/18
2/24/25, 11:29 AM Create a Simple Blog Application Using Django - DEV Community

Step 2: Install Django


Once your virtual environment is activated, install Django using pip, Python's
package installer.

pip install django

https://dev.to/kihuni/create-a-simple-blog-application-using-django-4fo9 2/18
2/24/25, 11:29 AM Create a Simple Blog Application Using Django - DEV Community

Step 3: Set Up the Project and App


Create a Django project:
Create your django project and cd into it.

mkdir Simple-blog
django-admin startproject myblog
cd myblog

https://dev.to/kihuni/create-a-simple-blog-application-using-django-4fo9 3/18
2/24/25, 11:29 AM Create a Simple Blog Application Using Django - DEV Community

After running the django-admin startproject you should have the following
structure:

myblog/
manage.py
myblog/
__init__.py
settings.py
urls.py
wsgi.py
asgi.py

Create a Django App:


Inside your django project folder, run the following command to create an app:

# Create a new app named 'blog'


python manage.py startapp blog

https://dev.to/kihuni/create-a-simple-blog-application-using-django-4fo9 4/18
2/24/25, 11:29 AM Create a Simple Blog Application Using Django - DEV Community

Configure the App


To use the new app in your project, add it to the INSTALLED_APPS list in the
settings.py file.

# myblog/settings.py

INSTALLED_APPS = [
...
'blog',
]

After following all instructions, you should have the complete structure as follows:

myblog/
manage.py
myblog/
__init__.py
settings.py
urls.py
wsgi.py
asgi.py
blog/
__init__.py
https://dev.to/kihuni/create-a-simple-blog-application-using-django-4fo9 5/18
2/24/25, 11:29 AM Create a Simple Blog Application Using Django - DEV Community

admin.py
apps.py
models.py
tests.py
views.py
migrations/

Step 4: Define the model


On the model file blog/models.py add the following to define the post model:

from django.db import models

class Post(models.Model):
title = models.CharField(max_length=100)
content = models.TextField()
published_date = models.DateTimeField(auto_now_add=True)

def __str__(self):
return self.title

Run Migrations:
Migrations in Django are like version control for your database schema. They're a
way to apply and un-apply changes to your database schema, which include
creating new tables, changing fields, or deleting tables. Django creates these
migration files for you, and you can apply them using the makemigrations and

https://dev.to/kihuni/create-a-simple-blog-application-using-django-4fo9 6/18
2/24/25, 11:29 AM Create a Simple Blog Application Using Django - DEV Community

migrate management commands. This system allows Django to automatically


provide schema alteration operations when new changes are made to models.
The makemigrations command in Django creates new migration files based on the
changes you've made to your models. It doesn't affect your database yet.
On the other hand, the migrate command applies these migrations to your
database. It reads the migration files and applies the corresponding changes to
your database schema.

To run migrations:

python manage.py makemigrations


python manage.py migrate

N/b Remember, makemigrations should be run when you've made changes to your
models and want to create new migrations, and migrate should be run to apply these
migrations to your database.

Step 5: Register the model with Admin


On the admin file blog/admin.py add the following to register your models with
admin:

https://dev.to/kihuni/create-a-simple-blog-application-using-django-4fo9 7/18
2/24/25, 11:29 AM Create a Simple Blog Application Using Django - DEV Community

from django.contrib import admin


from .models import Post

admin.site.register(Post)

Step 6: Create a superuser


Run the following command on your terminal and follow the steps to create a
superuser.

python manage.py createsuperuser

https://dev.to/kihuni/create-a-simple-blog-application-using-django-4fo9 8/18
2/24/25, 11:29 AM Create a Simple Blog Application Using Django - DEV Community

Run the server using python manage.py runserver ,


navigate to
http://127.0.0.1:8000/admin/ , and use your already created superuser credentials
to access the admin site.

Step 7: Define the View


https://dev.to/kihuni/create-a-simple-blog-application-using-django-4fo9 9/18
2/24/25, 11:29 AM Create a Simple Blog Application Using Django - DEV Community

On the views file blog/views.py add the following to create your app views:

from django.shortcuts import render


from .models import Post

def home(request):
posts = Post.objects.all()
return render(request, 'blog/home.html', {'posts': posts})

Step 8: Configure the URL


Create a blog/urls.py file and add the following to map the URL to your view:

from django.urls import path


from blog import views

urlpatterns = [
path('home/', views.home, name='home'),
]

https://dev.to/kihuni/create-a-simple-blog-application-using-django-4fo9 10/18
2/24/25, 11:29 AM Create a Simple Blog Application Using Django - DEV Community

Then, register your app URLs blog/urls.py to your django project in


myblog/urls.py

from django.contrib import admin


from django.urls import path, include

urlpatterns = [
path('admin/', admin.site.urls),
path('', include('blog.urls')), # Add this line
]

https://dev.to/kihuni/create-a-simple-blog-application-using-django-4fo9 11/18
2/24/25, 11:29 AM Create a Simple Blog Application Using Django - DEV Community

Step 9: Create the Template


Create blog/templates/blog/home.html and the following to structure and present
our blog as a webpage:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Blog Home</title>
</head>
<body>
<h1>Blog Posts</h1>
{% for post in posts %}
<h2>{{ post.title }}</h2>
<p>{{ post.content }}</p>
<p>Published on: {{ post.published_date }}</p>
{% endfor %}
</body>
</html>

https://dev.to/kihuni/create-a-simple-blog-application-using-django-4fo9 12/18
2/24/25, 11:29 AM Create a Simple Blog Application Using Django - DEV Community

Step 10: Run the Development Server


Finally, let's run our development server using the following command:

python manage.py runserver

Navigate to http://127.0.0.1:8000/home/ to see your simple blog application in


action.

https://dev.to/kihuni/create-a-simple-blog-application-using-django-4fo9 13/18
2/24/25, 11:29 AM Create a Simple Blog Application Using Django - DEV Community

Use the admin site http://127.0.0.1:8000/admin/ to create a blog post and see it
live on your simple blog.

Conclusion and Next Steps


By understanding Django's project structure, MVT architecture, and the Django
admin interface, you now have the foundational knowledge to build web
https://dev.to/kihuni/create-a-simple-blog-application-using-django-4fo9 14/18
2/24/25, 11:29 AM Create a Simple Blog Application Using Django - DEV Community

applications. In the next part of this series, we'll dive deeper into Django's ORM,
explore how to create more complex views and templates and implement additional
functionality to enhance our blog application. Stay tuned!

Timescale PROMOTED

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM


Make Vector Search Simple
We built pgai Vectorizer to simplify embedding management for AI applications
—without needing a separate database or complex infrastructure. Since launch,
developers have created over 3,000 vectorizers on Timescale Cloud, with many
more self-hosted.

Read more

Read More

Top comments (0)


Code of Conduct • Report abuse

Postmark PROMOTED

https://dev.to/kihuni/create-a-simple-blog-application-using-django-4fo9 15/18
2/24/25, 11:29 AM Create a Simple Blog Application Using Django - DEV Community

Speedy emails, satisfied customers


Are delayed transactional emails costing you user satisfaction? Postmark
delivers your emails almost instantly, keeping your customers happy and
connected.

Sign up

kihuni

https://dev.to/kihuni/create-a-simple-blog-application-using-django-4fo9 16/18
2/24/25, 11:29 AM Create a Simple Blog Application Using Django - DEV Community

Software Developer specializing in backend Systems and Technical Writing.

LOCATION
Nairobi, kenya
PRONOUNS
Mr
JOINED
Mar 9, 2021

More from kihuni

Building a REST API with Django REST Framework: A Beginners Guide


#webdev #python #django #beginners

I Just Found Out You Can Switch Search Engines—Here’s How!


#webdev #beginners #python #django

What is a RESTful API? A Beginner’s Guide


#webdev #api #django #python

Timescale PROMOTED

Timescale – the developer's data platform for modern apps,


built on PostgreSQL

https://dev.to/kihuni/create-a-simple-blog-application-using-django-4fo9 17/18
2/24/25, 11:29 AM Create a Simple Blog Application Using Django - DEV Community

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT,
AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

https://dev.to/kihuni/create-a-simple-blog-application-using-django-4fo9 18/18

You might also like