Using Django in Python A Comprehensive Guide

Django is a high-level, open-source web framework that allows developers to build clean, maintainable, and efficient web applications quickly using Python. It follows the Model-View-Template (MVT) architectural pattern and emphasizes reusability and “plug-ability” of components. In this article, we will delve into the key concepts of Django and demonstrate how to create a simple web application using this powerful framework.
Table of Contents
Section titled “Table of Contents”- Installation and Setup
- Creating a Django Project
- Creating a Django App
- Models and the Database
- Views and URL Routing
- Templates and Static Files
- Conclusion
Installation and Setup
Section titled “Installation and Setup”To get started with Django, you first need to install it. We recommend creating a virtual environment to isolate the dependencies of your project. Run the following commands in your terminal:
$ python -m venv myenv$ source myenv/bin/activate ## On Windows, use 'myenv\Scripts\activate'$ pip install djangoOnce Django is installed, you can verify the installation by running:
$ python -m django --versionThis should display the version of Django you have installed.
Creating a Django Project
Section titled “Creating a Django Project”A Django project is a collection of configurations and apps for a specific web application. To create a new project, run the following command:
$ django-admin startproject myprojectThis will generate a new directory called myproject with the following structure:
myproject/ manage.py myproject/ __init__.py asgi.py settings.py urls.py wsgi.pyThe top-level myproject directory is just a container, and its name doesn’t matter to Django. You can rename it if you wish.
Creating a Django App
Section titled “Creating a Django App”A Django app is a self-contained module that encapsulates a specific functionality of your web application. To create a new app, navigate to your project’s root directory (where manage.py is located) and run:
$ python manage.py startapp myappThis will generate a new directory called myapp with the following structure:
myapp/ __init__.py admin.py apps.py migrations/ __init__.py models.py tests.py views.pyModels and the Database
Section titled “Models and the Database”A model is a Python class that defines the structure of your application’s data. In Django, models are used to interact with the database. Let’s create a simple model for a blog post:
## myapp/models.py
from django.db import models
class Post(models.Model): title = models.CharField(max_length=200) content = models.TextField() pub_date = models.DateTimeField('date published')
def __str__(self): return self.titleTo create the database tables for your models, run the following commands:
$ python manage.py makemigrations myapp$ python manage.py migrateViews and URL Routing
Section titled “Views and URL Routing”A view is a Python function that takes a web request and returns a web response. In Django, views are used to define what should be displayed on a particular page. Let’s create a simple view that returns a list of all blog posts:
## myapp/views.py
from django.http import HttpResponsefrom .models import Post
def index(request): posts = Post.objects.order_by('-pub_date') output = ', '.join([p.title for p in posts]) return HttpResponse(output)To map a URL to your view, you need to create a URL configuration for your app and include it in the project’s URL configuration:
## myapp/urls.py
from django.urls import pathfrom . import views
urlpatterns = [ path('', views.index, name='index'),]
## myproject/urls.py
from django.contrib import adminfrom django.urls import include, path
urlpatterns = [ path('myapp/', include('myapp.urls')), path('admin/', admin.site.urls),]Templates and Static Files
Section titled “Templates and Static Files”Templates are text files that define the structure and layout of your HTML pages. They can include placeholders for dynamic content, which are replaced with actual data when the template is rendered. Let’s create a simple template for our blog post view:
{% for post in posts %} <h2>{{ post.title }}</h2> <p>{{ post.content }}</p> <p>Published on {{post.pub_date }}</p>{% endfor %}To use this template in your view, you need to update the view to render the template with the required context:
## myapp/views.py
from django.shortcuts import renderfrom .models import Post
def index(request): posts = Post.objects.order_by('-pub_date') context = {'posts': posts} return render(request, 'myapp/index.html', context)Static files are files that are not dynamically generated, such as CSS, JavaScript, and images. To serve static files in your Django app, create a directory named static inside your app’s folder and place your static files there. Then, update your template to reference the static files:
{% load static %}<link rel="stylesheet" type="text/css" href="{% static 'myapp/style.css' %}">
{% for post in posts %} <h2>{{ post.title }}</h2> <p>{{ post.content }}</p> <p>Published on {{ post.pub_date }}</p>{% endfor %}Conclusion
Section titled “Conclusion”In this article, we introduced Django, a powerful web framework for building web applications using Python. We covered the key concepts of Django, including projects, apps, models, views, URL routing, templates, and static files. With this foundation, you can now start building your own web applications using Django. To further expand your knowledge, we recommend exploring Django’s documentation and experimenting with more advanced features, such as forms, user authentication, and deployment.
© 2026 David Li · Home · Blog · RSS ·Classic site · Built with Astro, Starlight and three.js