Django is a high-level Python web framework that enables rapid development of secure and maintainable websites. Built by experienced developers, it takes care of much of the hassle of web development, so you can focus on writing your app without reinventing the wheel.
Why Django?
-
Batteries included: Comes with built-in features like ORM, authentication, admin panel, forms, and more.
-
Security: Protects against many attacks like SQL injection, CSRF, XSS, etc.
-
Scalability: Used by large sites like Instagram, Pinterest, and Disqus.
-
Versatile: Suitable for everything from simple blogs to complex enterprise systems.
⚙️ Step-by-Step: Getting Started with Django
1. Install Django
First, ensure you have Python installed. Create a virtual environment:
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
Install Django:
pip install django
Verify installation:
django-admin --version
2. Create a Django Project
django-admin startproject mysite
cd mysite
This creates a directory structure like:
mysite/
manage.py
mysite/
__init__.py
settings.py
urls.py
asgi.py
wsgi.py
-
manage.py
: Command-line utility for managing the project. -
settings.py
: Configuration file. -
urls.py
: Root URL configuration.
3. Run Development Server
python manage.py runserver
Visit http://127.0.0.1:8000/
– you’ll see Django’s welcome page.
4. Create an App
Django projects are made up of apps.
python manage.py startapp blog
Folder structure:
blog/
admin.py
apps.py
models.py
views.py
urls.py (you need to create this)
...
5. ✍️ Define a Model
In blog/models.py
:
from django.db import models
class Post(models.Model):
title = models.CharField(max_length=100)
content = models.TextField()
created_at = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.title
6. ⚙️ Register App and Migrate
Add 'blog'
to INSTALLED_APPS
in mysite/settings.py
.
Then run:
python manage.py makemigrations
python manage.py migrate
This applies changes to the database.
7. Create Admin User and Register Model
python manage.py createsuperuser
In blog/admin.py
:
from django.contrib import admin
from .models import Post
admin.site.register(Post)
Start the server and go to http://127.0.0.1:8000/admin
to log in.
8. Create Views and URLs
Create a view in blog/views.py
:
from django.http import HttpResponse
from .models import Post
def home(request):
posts = Post.objects.all()
output = "<br>".join([post.title for post in posts])
return HttpResponse(output)
Set up blog/urls.py
:
from django.urls import path
from . import views
urlpatterns = [
path('', views.home, name='home'),
]
Include app URLs in project’s mysite/urls.py
:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('blog.urls')),
]
Full Working Example: A Mini Blog
Structure:
mysite/
├── blog/
│ ├── models.py
│ ├── views.py
│ ├── urls.py
│ └── admin.py
├── mysite/
│ ├── settings.py
│ ├── urls.py
├── db.sqlite3
models.py
from django.db import models
class Post(models.Model):
title = models.CharField(max_length=100)
content = models.TextField()
created_at = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.title
admin.py
from django.contrib import admin
from .models import Post
admin.site.register(Post)
views.py
from django.shortcuts import render
from .models import Post
def home(request):
posts = Post.objects.all()
return render(request, 'blog/home.html', {'posts': posts})
urls.py
in blog
from django.urls import path
from . import views
urlpatterns = [
path('', views.home, name='home'),
]
urls.py
in mysite
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('blog.urls')),
]
Template: blog/templates/blog/home.html
<!DOCTYPE html>
<html>
<head>
<title>My Blog</title>
</head>
<body>
<h1>Blog Posts</h1>
<ul>
{% for post in posts %}
<li><strong>{{ post.title }}</strong>: {{ post.content }}</li>
{% endfor %}
</ul>
</body>
</html>
Make sure you add 'DIRS': [BASE_DIR / 'templates'],
to the TEMPLATES setting in settings.py
and create the folder structure if needed.
✅ Tips for Beginners
-
Use virtual environments to isolate dependencies.
-
Use the admin panel to easily manage data in early development.
-
Understand the MVC (or MVT) pattern in Django: Model-View-Template.
-
Explore Django REST Framework if you plan to build APIs.
⚠️ Common Pitfalls
Pitfall | Fix |
---|---|
❌ Forgetting to add app to INSTALLED_APPS |
✅ Always add your app to settings.py |
❌ Missing migrations | ✅ Run makemigrations and migrate |
❌ Template not found | ✅ Check template paths and TEMPLATES setting |
❌ View not loading | ✅ Ensure URL configuration includes the app’s urls.py |
❌ Static files not showing | ✅ Use collectstatic and proper STATIC_URL setup for production |
Conclusion
Django provides a powerful, clean, and pragmatic way to build web applications using Python. Whether you're building a simple blog or a complex system, Django can scale with you. This guide covered the basics—models, views, URLs, admin, and templates—and gave you a full mini blog example to start with.