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.

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.

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.

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

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

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

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.



















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/
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
-
{% 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.