› Forums › Web Development › HarvardX: CS50W – CS50’s Web Programming with Python and JavaScript › CS50W – Lecture 3 – Django › Understanding View Functions in Django: How Web Pages Really Work
- This topic is empty.
-
AuthorPosts
-
February 20, 2026 at 5:52 pm #6128
When learning Django, one of the first important concepts you must understand is the view function.
Many beginners can write templates and URLs, but still feel confused about:
❓ What exactly is a view?
❓ Why do we need it?
❓ How does it control a web page?In this article, we’ll answer these questions in a simple and practical way.
✅ What Is a View Function?
In Django, a view function is:
👉 A Python function that decides what the user sees when they open a web page.
In simple words:
🧠 A view is the brain of a page.
Every page you see in a Django website is controlled by a view.
🌐 How a Web Page Works in Django
Whenever a user opens a page, this happens:
User → URL → View → HTML → BrowserStep by step:
1️⃣ User opens a link
2️⃣ Django finds the URL
3️⃣ Django runs a view
4️⃣ View returns a page
5️⃣ Browser displays itWithout a view, Django cannot show any page.
📌 A Simple View Example
Here is the most basic view:
def index(request): return render(request, "tasks/index.html")This means:
👉 “When this view is called, show
index.html.”Let’s break it down.
1️⃣
requestdef index(request):The
requestcontains information about the user:- Is the user logged in?
- Did they submit a form?
- What browser are they using?
- What data did they send?
Every view always receives
request.
2️⃣
render()return render(request, "tasks/index.html")render()means:“Take this HTML file and display it in the browser.”
So this line sends
index.htmlto the user.
🧩 Why Is It Called a “View”?
Because it controls what the user views (sees).
Example:
Page View Name Task List index Add Task add Login login Profile profile Each major page usually has one main view.
✅ View With Data (Real Example)
Most pages don’t just show static HTML.
They also show data.Example:
def index(request): tasks = ["Buy milk", "Study Django", "Write blog"] return render(request, "tasks/index.html", { "tasks": tasks })Here:
tasksis a Python list- It is sent to the template
- The template displays it
In
index.html:{% for task in tasks %} <li>{{ task }}</li> {% endfor %}So:
👉 View prepares data
👉 Template displays dataThey work together.
📝 View for Forms (Add Task Example)
For forms, views do two jobs:
✔ Show the form
✔ Process the formExample:
def add(request): if request.method == "POST": task = request.POST["task"] # Save task here return render(request, "tasks/add.html")This view:
- Shows the form (GET request)
- Receives submitted data (POST request)
- Saves it
- Returns a page
One function does both.
🔗 How Views Connect to URLs
Views only work when connected to URLs.
In
urls.py:urlpatterns = [ path("", views.index, name="index"), path("add", views.add, name="add"), ]This means:
URL View /tasks/ index() /tasks/add add() So:
- Opening
/tasks/→ runsindex() - Opening
/tasks/add→ runsadd()
URLs decide which view runs.
🧠 The Django Mental Model (Very Important)
Always remember:
URL → View → TemplateExample:
/tasks/ → index() → index.html /tasks/add → add() → add.htmlThe view is the middleman.
📏 “One Page = One View” Rule
In most Django projects:
Each main page has its own view.
Example:
Page View Home home() Tasks index() Add Task add() Login login() This keeps code:
✔ Clean
✔ Organized
✔ Easy to maintain
🔄 Can One View Do Multiple Things?
Yes.
Example with GET and POST:
def add(request): if request.method == "GET": # Show form if request.method == "POST": # Save dataSame page. Same view. Different behavior.
This is very common in Django.
🏗️ Why Views Are So Important
Views are important because they:
✅ Control navigation
✅ Handle form data
✅ Connect database to templates
✅ Manage users
✅ Control securityIn real projects, most logic lives in views.
🎯 Final Definition (For Beginners)
A view function in Django is a Python function that receives a user request, processes data, and returns an HTML page as a response.
Or simply:
A view decides what the user sees.
✅ Summary
Let’s summarize what you learned:
✔ A view is the brain of a page
✔ Every page needs a view
✔ Views receive requests
✔ Views return HTML
✔ Views send data to templates
✔ URLs decide which view runs
📌 One-Line Reminder
User opens link → Django runs view → View returns page → User sees result
Understanding view functions is the foundation of Django development. Once you master this, learning forms, databases, and authentication becomes much easier.
-
AuthorPosts
- You must be logged in to reply to this topic.

