Django Python Example: Build Web Apps Fast

Logan Jul 13, 2026

Django, a high-level Python web framework, is renowned for its rapid development, clean, pragmatic design, and ability to follow the "batteries-included" philosophy. It's an excellent choice for building web applications with Python, simplifying the process with a robust set of features and tools. Let's delve into a practical Django example to illustrate its power and ease of use.

đź”— Django Models: OneToOneField (One-to-One Relationship)
đź”— Django Models: OneToOneField (One-to-One Relationship)

Before we dive into a Django Python example, ensure you have Python and pip installed. Then, install Django using pip: `pip install Django`. Once installed, create a new Django project and navigate to its directory. Here's a step-by-step guide to creating a simple 'To-Do' web application.

Django is a high-level Python framework that automates much of web development by providing tools for database management, URL routing, templates, and security. It helps developers build scalable and secure applications quickly.  đź’ˇhttps://www.pybeginners.com  #Python #DjangoFramework #WebApps Django Python, Django Framework, Programming Humor, Basic Computer Programming, Learn Computer Coding, Computer Coding, Coding Languages, Weird Science, Web Design Tips
Django is a high-level Python framework that automates much of web development by providing tools for database management, URL routing, templates, and security. It helps developers build scalable and secure applications quickly. đź’ˇhttps://www.pybeginners.com #Python #DjangoFramework #WebApps Django Python, Django Framework, Programming Humor, Basic Computer Programming, Learn Computer Coding, Computer Coding, Coding Languages, Weird Science, Web Design Tips

Setting Up the Django Project

After creating a new Django project, you'll find a basic project structure. Let's create an app within this project – we'll call it 'tasks'. Django apps are reusable components with their own models, views, templates, and URLs.

| Pythonista Planet
| Pythonista Planet

To create the 'tasks' app, run `python manage.py startapp tasks`. Now, our project structure includes 'tasks', the parent 'project', and 'manage.py', a script for administrative tasks.

Defining the To-Do Item Model

DJANGO VS FASTAPI (2026)
DJANGO VS FASTAPI (2026)

In Django, a model is an application-specific representation of data. We'll define our 'Task' model in 'tasks/models.py'. It will have fields like 'title' and 'description', along with creation and completion timestamps.

The 'Task' model in 'tasks/models.py' might look like this: ```python from django.db import models from django.utils import timezone class Task(models.Model): title = models.CharField(max_length=200) description = models.TextField() created_at = models.DateTimeField( auto_now_add=True) completed_at = models.DateTimeField(blank=True, null=True) ``` This code creates a 'Task' model with four fields. The 'created_at' field automatically sets the current time when a new task is created, while 'completed_at' is optional and can be left blank.

Migrating the New Model

the diagram shows how to use django
the diagram shows how to use django

After defining the 'Task' model, we need to create a database schema for it. Django uses a command-line tool called 'makemigrations' to generate migration files and 'migrate' to apply them to the database.

Run `python manage.py makemigrations tasks` to create migration files, then apply them with `python manage.py migrate`. This sets up the database schema for our 'Task' model.

Creating Views and URLs for the To-Do App

Secure Your Django Project with .env Files
Secure Your Django Project with .env Files

Views in Django are essentially functions that take a web request and return a web response. They determine which content should be sent back to the client. Now, let's create views for listing, adding, and completing tasks in 'tasks/views.py'.

Here's a simple implementation: ```python from django.shortcuts import render, redirect from .models import Task def index(request): tasks = Task.objects.all() return render(request, 'tasks/index.html', {'tasks': tasks}) def add_task(request): if request.method == 'POST': Task.objects.create(title=request.POST['title'], description=request.POST['description']) return redirect('index') def complete_task(request, pk): task = Task.objects.get(pk=pk) task.completed_at = timezone.now() task.save() return redirect('index') ``` The 'index' view retrieves all tasks and passes them to the 'index.html' template. The 'add_task' view creates a new task from POST data and redirects to the index page. The 'complete_task' view marks a task as complete based on the provided primary key.

Ebook: Introducing the Django Getting Started with Python Basics
Ebook: Introducing the Django Getting Started with Python Basics
a flow diagram with different types of data and information in it, including the text
a flow diagram with different types of data and information in it, including the text
Python cheat sheet 23 (Django)
Python cheat sheet 23 (Django)
Tango with Django
Tango with Django
Django vs Flask: Which Python Framework Should You Learn First?
Django vs Flask: Which Python Framework Should You Learn First?
How Much Python Should You know To Learn Django?
How Much Python Should You know To Learn Django?
Django Deployment Simplified
Django Deployment Simplified
All Important Python Functions for Beginners (Complete Cheat Sheet)
All Important Python Functions for Beginners (Complete Cheat Sheet)
Pizza Ordering System Project in Django with Source Code
Pizza Ordering System Project in Django with Source Code
Learn Django Project  and File Structure
Learn Django Project and File Structure
Python Django Online Food Ordering System with Source Code
Python Django Online Food Ordering System with Source Code
Django Template Language (DTL) Basics
Django Template Language (DTL) Basics
Common Django Mistakes Beginners Make (Avoid These!)
Common Django Mistakes Beginners Make (Avoid These!)
Python cheat sheet 25 (Django 2)
Python cheat sheet 25 (Django 2)
đź“„ Template with Model Relations in Django
đź“„ Template with Model Relations in Django
Django Models – choices Option
Django Models – choices Option
Unveiled Secret! Elite Python, Java, JS Expertise: Stop Searching, Start Shining – Assured Mastery!
Unveiled Secret! Elite Python, Java, JS Expertise: Stop Searching, Start Shining – Assured Mastery!
Python Django for Web Apps: Beginner’s Overview
Python Django for Web Apps: Beginner’s Overview
⚙️ Django Generic Views – Build Faster with Less Code
⚙️ Django Generic Views – Build Faster with Less Code

Next, we need to map these views to URLs. In 'tasks/urls.py', include the following: ```python from django.urls import path from . import views urlpatterns = [ path('', views.index, name='index'), path('add_task/', views.add_task, name='add_task'), path('complete_task//', views.complete_task, name='complete_task'), ] ``` Now, the correct view will be called for each URL pattern.

Creating Templates for the To-Do App

Templates allow you to define reusable chunks of HTML that will be rendered for the user. Create a new 'index.html' file in 'tasks/templates/tasks/' with the following content: ```html To-Do List

To-Do List

{% csrf_token %}

    {% for task in tasks %}
  • {{ task.title }} - {{ task.description }} - Completed: {{ task.completed_at }}
  • Mark as Complete {% empty %} No tasks found. {% endfor %}

``` This template contains a form to add new tasks and a list to display existing tasks. It uses Django's template language to generate the list dynamically.

You're now ready to run the application with `python manage.py runserver` and navigate to 'http://localhost:8000' in your browser. You'll see a simple to-do list where you can add tasks and mark them as complete.

Exploring Django with this 'To-Do' web application example demonstrates how quickly you can build intuitive and functional web apps using Python. Now that you've understood the basics, you can expand and customize this project to fit your needs, or start your next Django project with confidence.