Create a Simple Blog Application Using Django - DEV Community
Create a Simple Blog Application Using Django - DEV Community
kihuni
Posted on Jul 26, 2024
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
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
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
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
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
# 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/
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
To run migrations:
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.
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
admin.site.register(Post)
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
On the views file blog/views.py add the following to create your app views:
def home(request):
posts = Post.objects.all()
return render(request, 'blog/home.html', {'posts': posts})
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
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
<!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
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.
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
Read more
Read More
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
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
LOCATION
Nairobi, kenya
PRONOUNS
Mr
JOINED
Mar 9, 2021
Timescale PROMOTED
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