Version: 3.0 | Last Updated: February 25, 2026
You can install multiple Python versions on the same system. If you already have Python installed, use the py launcher to install another version alongside it:
Virtual environments create isolated Python environments for your projects:
To create an environment with a specific Python version:
When you're done working on your project, deactivate the virtual environment:
Check if Django is installed; if not, install it using pip:
Replace project_name with your desired project name (e.g., mywebsite, myproject).
| App Purpose | Description |
|---|---|
| Blog | Handles blog posts, comments, and categories |
| Shop | Manages products, cart, and orders |
| User Accounts | Handles user registration, login, and profiles |
In your app folder, create a file named urls.py:
yourapp/
├── __init__.py
├── admin.py
├── apps.py
├── models.py
├── tests.py
├── urls.py <-- Create this file
└── views.py
In yourapp/views.py, create your views:
from django.http import HttpResponse
def index(request):
return HttpResponse("<h1>Hello, this is the index page of myApp!</h1>")
def blog(request):
return HttpResponse("<h1>Welcome to the Blog Page</h1>")
def about(request):
return HttpResponse("<h1>About Us</h1><p>This is our company information.</p>")
HttpResponse for testing. Normally, we use render() to return templates with HTML files.
In yourapp/urls.py, set up the URL patterns:
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
path('blog/', views.blog, name='blog'),
path('about/', views.about, name='about'),
]
In your project's main urls.py file:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('yourapp.urls')), # Connect your app URLs to the project
]
'' in the project's urls.py means the paths start at the root, so blog becomes http://127.0.0.1:8000/blog/. If you used 'home/' in project's urls.py, the paths would start at http://127.0.0.1:8000/home/. Test it then you will understand.
Now run the development server to see your pages in action:
Open your browser and visit these URLs:
Purpose: Enables Models, Templates, and Features
settings.py.INSTALLED_APPS list.'myapp',.Purpose: Defining Tables in the Database
models.py.Example Model:
from django.db import models
class Product(models.Model):
name = models.CharField(max_length=100)
price = models.DecimalField(max_digits=10, decimal_places=2)
description = models.TextField()
created_at = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.name
This tells Django that something has changed in the models.
This updates those changes in the database.
Parameters are used to identify and access a specific resource (such as a single product, user, or post) for CRUD operations (Create, Read, Update, Delete).
Go to your app's urls.py file. Pass parameters in the URLs where you want to use them:
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
path('product/<int:id>/', views.product_detail, name='product_detail'),
path('category/<str:category_name>/', views.category_products, name='category_products'),
]
| Parameter Type | Example | Description |
|---|---|---|
<int:id> |
/product/5/ | Matches an integer and passes it as 'id' |
<str:name> |
/category/electronics/ | Matches a string and passes it as 'name' |
<slug:slug> |
/post/my-first-post/ | Matches a slug (letters, numbers, hyphens) |
Go to your app's views.py file. Import your models and use the parameter to retrieve specific data:
from django.http import HttpResponse
from .models import Product
def product_detail(request, id):
# Use the parameter to find a matching product
try:
product = Product.objects.get(id=id)
return HttpResponse(f"<h1>{product.name}</h1><p>Price: ${product.price}</p>")
except Product.DoesNotExist:
return HttpResponse("<h1>Product not found</h1>")
def category_products(request, category_name):
# Find all products in a category
products = Product.objects.filter(category=category_name)
return HttpResponse(f"<h1>Products in {category_name}</h1><p>Found {products.count()} products</p>")
Open your browser and go to http://127.0.0.1:8000/admin/ (remember the admin path we already have in project/urls.py)
From the admin panel, you can:
admin.py file in your app.admin.site.register().Example admin.py:
from django.contrib import admin
from .models import Product # Import your model
# Register your models here
admin.site.register(Product)
Templates in Django are HTML files that can include dynamic content. They allow you to separate presentation (frontend) from logic (backend).
Inside your Django app, create a folder named templates. You can organize multiple HTML files here.
Components are reusable HTML parts (like header/footer).
Blocks (or slots) are placeholders for dynamic content that child templates can fill.
Open the views.py file in your app and render your template using the render() function:
from django.shortcuts import render
def home(request):
# You can pass data to the template
context = {
'site_name': 'My Awesome Site',
'year': 2026
}
return render(request, 'pages/home.html', context)
def about(request):
return render(request, 'pages/about.html')
In your app's urls.py, create URL paths for these views:
from django.urls import path
from . import views
urlpatterns = [
path('', views.home, name='home'),
path('about/', views.about, name='about'),
]
Open your browser and navigate to:
You can pass data from your view to templates using a context dictionary:
def product_list(request):
# Get data from database
products = Product.objects.all()
# Pass to template
context = {
'products': products,
'total_products': products.count(),
'title': 'Our Products'
}
return render(request, 'pages/products.html', context)
Then in your template, you can use this data:
| Command | Purpose |
|---|---|
py install 3.13 |
Install Python 3.13 using py launcher |
python -m venv env |
Create virtual environment |
env\Scripts\activate |
Activate virtual environment (Windows) |
deactivate |
Deactivate virtual environment |
pip install django |
Install Django |
django-admin startproject project_name |
Create new Django project |
python manage.py startapp app_name |
Create new Django app |
python manage.py makemigrations |
Create migrations for model changes |
python manage.py migrate |
Apply migrations to database |
python manage.py createsuperuser |
Create admin user for the admin panel |
python manage.py runserver |
Start development server |
admin.py to manage them through the admin interface{% include %} tag for reusable components like headers, footers, and buttons{% block %} and {% endblock %} to create flexible layoutsThis guide provides a complete foundation for Django development. Continue learning about forms, authentication, and REST APIs for full-stack development.
Happy Coding with Django! 🚀