› Forums › Web Development › HarvardX: CS50W – CS50’s Web Programming with Python and JavaScript › CS50W – Lecture 3 – Django › Learning Django Forms: How to Add and Save Data Using a Single Page
- This topic is empty.
-
AuthorPosts
-
February 20, 2026 at 5:25 pm #6126
Below is a complete learning tutorial you can publish for your audience.
π Learning Django Forms: How to Add and Save Data Using a Single Page
In this tutorial, weβll learn how to use forms in Django to collect user input, send it to the server, and store it in a database.
We will build a simple Task Manager where users can add tasks using a form.
This approach is commonly taught in Django tutorials and courses like CS50βs Web Programming with Python and JavaScript.
π What You Will Learn
By the end of this tutorial, you will understand:
β How forms work in Django
β What βsubmitting to the same pageβ means
β How URLs, views, and templates connect
β How to save submitted data
β How Django knows which user submitted what
π Project Structure (Context)
A typical Django project looks like this:
project/ β βββ project/ β Main project folder β βββ urls.py β βββ tasks/ β App folder β βββ views.py β βββ models.py β βββ urls.py β βββ templates/ β βββ tasks/ β βββ index.html β βββ add.html β βββ manage.pyWe will work mainly inside:
tasks/views.pytasks/urls.pytasks/templates/tasks/models.py
π§© Step 1: Creating the Add View (views.py)
Open:
tasks/views.pyAdd this:
from django.shortcuts import render, redirect from .models import Task # Show form and handle submission def add(request): if request.method == "POST": task_text = request.POST["task"] Task.objects.create( user=request.user, title=task_text ) return redirect("index") return render(request, "tasks/add.html")Explanation
This function does two jobs:
Request Type Action GET Shows the form POST Saves the data So one page both displays and processes the form.
π§© Step 2: Creating URLs (urls.py)
Inside the App
Create:
tasks/urls.pyAdd:
from django.urls import path from . import views urlpatterns = [ path("add", views.add, name="add"), ]
Inside Main Project
Open:
project/urls.pyAdd:
from django.urls import path, include urlpatterns = [ path("tasks/", include("tasks.urls")), ]
Final URL
Now your form page is available at:
/tasks/add
π§© Step 3: Creating the Form Template (add.html)
Create file:
tasks/templates/tasks/add.htmlAdd:
<!DOCTYPE html> <html lang="en"> <head> <title>Add Task</title> </head> <body> <h1>Add Task</h1> <form action="" method="post"> {% csrf_token %} <input type="text" name="task" placeholder="Enter task" required> <input type="submit" value="Save"> </form> </body> </html>
Explanation
1οΈβ£
action=""<form action="">Means:
π Submit to the same URL.
If page is:
/tasks/addForm submits to:
/tasks/addAgain.
2οΈβ£
method="post"method="post"Uses secure POST method (recommended).
3οΈβ£
{% csrf_token %}{% csrf_token %}Protects against hacking attacks.
Required in Django forms.
4οΈβ£
name="task"<input name="task">This connects to:
request.POST["task"]in views.
π§© Step 4: Creating the Model (models.py)
To store data permanently, we need a database model.
Open:
tasks/models.pyAdd:
from django.db import models from django.contrib.auth.models import User class Task(models.Model): user = models.ForeignKey( User, on_delete=models.CASCADE ) title = models.CharField(max_length=200) created_at = models.DateTimeField(auto_now_add=True) def __str__(self): return self.title
What This Does
It creates a table like:
Column Meaning user Who created task title Task text created_at Time
π§© Step 5: Apply Database Changes
Run:
python manage.py makemigrations python manage.py migrateThis creates database tables.
π§© Step 6: Showing Tasks (index view)
Open:
tasks/views.pyAdd:
def index(request): tasks = Task.objects.filter(user=request.user) return render(request, "tasks/index.html", { "tasks": tasks })
π§© Step 7: Display Tasks (index.html)
Create:
tasks/templates/tasks/index.htmlAdd:
<!DOCTYPE html> <html> <head> <title>My Tasks</title> </head> <body> <h1>My Tasks</h1> <ul> {% for task in tasks %} <li>{{ task.title }}</li> {% endfor %} </ul> <a href="/tasks/add">Add New Task</a> </body> </html>
π How Form Submission Works (Full Flow)
Letβs understand the complete process.
Step 1: User Opens Page
/tasks/addDjango β Shows form.
Step 2: User Submits
User enters:
Buy groceriesClicks Save.
Step 3: Browser Sends Data
POST request to:
/tasks/addWith:
task=Buy groceries
Step 4: Django Processes
In
views.py:if request.method == "POST":Data is saved in database.
Step 5: Redirect
User is sent to:
/tasks/and sees updated list.
π₯ How Django Knows βWho Submitted Whatβ
Django has a built-in login system.
When user logs in:
request.usercontains their identity.
So when we save:
Task.objects.create( user=request.user, title=task_text )Each task is linked to its owner.
ποΈ Where Is Data Stored?
By default, Django uses:
db.sqlite3File in project root.
Inside:
tasks_tasktable stores all records.
π How to See Submitted Data
Method 1: Admin Panel
Enable admin and visit:
/adminYou can see all tasks and users.
Method 2: Django Shell
python manage.py shellfrom tasks.models import Task Task.objects.all()
Method 3: Website
Each user sees their own tasks.
π Important URLs Summary
Purpose URL Task List /tasks/Add Task /tasks/addAdmin /admin
β Best Practices
Always follow these rules:
β Use POST for forms
β Use csrf_token
β Save data in models
β Use redirect after submit
β Link tasks to users
π― Final Summary
In this tutorial, you learned:
- How Django forms work
- What βsame page submissionβ means
- How views handle GET and POST
- How data is saved
- How users are tracked
- How URLs connect everything
This pattern is used in most professional Django projects.
-
AuthorPosts
- You must be logged in to reply to this topic.

