In this article, I’ll walk you through how I set up a simple Django project, with zero fluff and maximum clarity for building a fast, scalable web app.
Let’s build your first Django-powered website — step by step.Django is a high-level Python web framework that simplifies rapid web development.
Whether you are launching a business site or building a project, learning how to build a website using Django correctly is crucial for the long-term performance of your site.
Many beginners struggle to understand Django's models and forms. This comprehensive guide to Django models and forms ( with examples) will help you create robust web applications.
Why Choose Django for Creating a Website?
While creating a website, you will go through various phases like planning, design, development, deployment and scaling, which are essential for:
- Ensuring a smooth user experience from scratch.
- Building secure and maintainable backend architecture.
- Integrating SEO, analytics, and business tools.
- Structuring code for long-term flexibility.
As a bonus, Django simplifies all these phases with its batteries-included philosophy that enables you to focus more on features and user experience.
Prerequisites
Let's go through the requirements first:
- Basic Python knowledge: you needto understand Python basics, as Django is a Python framework.
- Python Installed: Make sure to install Python 3.8
- Code Editor and terminal access: Use a code editor like VS Code or PyCharm and have terminal/command line access to run Django commands.
Now let's break down the steps one by one with code examples-
Step 1: Set Up Your Project Folder
First things first: create a new folder for your Django project. I always like to keep things organized.
mkdir mysite
cd mysite
Next, create a virtual environment so you don't mess up your system Python. If you are setting up for the first time, then surely visit how to set up Django with a Virtual Environment.
python -m venv env
source env/bin/activate # On Windows: env\Scripts\activate
Install Django:
pip install django
You now have a clean workspace and Django ready to go.
✅ Also check out how I set up multiple Django environments for local and production projects.
Step 2: Create the Django Project and App
Now it’s time to bring Django to life.
django-admin startproject mysite
cd mysite
mysite
is the project folder that holds settings, URLs, etc.
Now, let’s make your first app — this is where the magic happens:
python manage.py startapp blog
Don’t forget to tell Django about your new app:
Open mysite/settings.py
, and add 'blog'
, to INSTALLED_APPS.
INSTALLED_APPS = [
...
'blog',
]
Step 3: Set Up URLs and Views
Let’s make your site display something — even if it’s just “Hello, world.”
In blog/views.py
, add a simple view
from django.http import HttpResponse
def home(request):
return HttpResponse("Welcome to the blog!")
Create a file in the blog folder called
urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.home, name='home'),
]
In
mysite/urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('blog.urls')),
]
Now run the server -
python manage.py runserver
http://127.0.0.1:8000
in your browser. 🎉 You should see your “Hello from Django!”
message.Step 4: Add HTML Templates
Let’s make that plain text a real HTML page.
Create a folder inside your blog
app
mkdir -p blog/templates/blog
index.html
inside that folder.<!DOCTYPE html>
<html>
<head>
<title>My Django Blog</title>
</head>
<body>
<h1>Welcome to My Blog!</h1>
</body>
</html>
blog/
templates/
blog/
index.html
from django.shortcuts import render
def home(request):
return render(request, 'blog/index.html')
Reload your browser. Boom — now you’ve got a real webpage!
Want to improve your templates with context data or layout inheritance? Stay tuned for my Django Templates Masterclass (coming soon)
Step 5: Set Up the Database
Let’s prepare Django’s database.
First, create your data model in blog/models.py
:
from django.db import models
class Post(models.Model):
title = models.CharField(max_length=100)
body = models.TextField()
created_at = models.DateTimeField(auto_now_add=True)
python manage.py makemigrations
python manage.py migrate
Your database is now set up and ready to store blog posts.
Step 6: Configure the Admin Panel
Django has a built-in admin panel. You just need to activate it.Create a superuser account.
python manage.py createsuperuser
blog/admin.py
:from django.contrib import admin
from .models import Post
admin.site.register(Post)
Now run the server again and visit-
http://127.0.0.1:8000/admin/
Log in and start managing content from a slick backend.
Step 7: Show Database Content on Your Page
Let’s show actual blog posts on your homepage.Update your view in
views.py
:from django.shortcuts import render
from .models import Post
def home(request):
posts = Post.objects.all().order_by('-created_at')
return render(request, 'blog/index.html', {'posts': posts})
Update index.html to loop through posts:
<body>
<h1>My Blog</h1>
{% for post in posts %}
<div>
<h2>{{ post.title }}</h2>
<p>{{ post.body }}</p>
<small>{{ post.created_at }}</small>
</div>
{% empty %}
<p>No posts yet.</p>
{% endfor %}
</body>
Now when you add blog posts from the admin panel, they’ll appear on your site automatically!
Level up with: How to Use Gunicorn with Django?
Final Thoughts
That’s it — you just built a working Django website with:
- Dynamic pages
- Templates
- A database-backed blog
- An admin panel
👉 Next up: How to Add Search to Your Django Site
👉 Or check out: Add Pagination Like a Pro
👉 Ready for Users? User Authentication in Django
If you're anything like me, this is where Django starts to click. From here, you can explore features like forms, APIs, or deployment.
For deploying existing Django project, this tutorial on Django set up with Gunicorn, PostgreSQL, and Nginx will assist you from scratch.
Common Mistakes to Avoid as a Beginner
- Not Using Django's Built-In Features: Use user authentication, admin interface, forms, and more.
- Ignoring the Project Structure: Follow best practices for structuring a Django project.
- Skipping Security Best Practices: Learn Django's middleware and settings.
FAQs
Q1: How do I add canonical tags in Django?
Using Django's template engine, you can add canonical tags in your base HTML template
<link rel="canonical" href="{{ request.build_absolute_uri }}">
Q2: How do I scale Django for high-traffic applications?
Scaling requires more than just hardware. Check out one of our guides on scaling Django for high-traffic applications.
Q3: How do i track changes in Django models for auditing?
You can track changes in Django models and maintain audit logs by using dedicated logging libraries that integrate with PostgreSQL. These tools help you monitor create
, update
, and delete
operations, offering better visibility and security in your Django application.