› Forums › Web Development › HarvardX: CS50W – CS50’s Web Programming with Python and JavaScript › CS50W – Lecture 3 – Django › Why We Use views.add Instead of views.add() in Django URLs
- This topic is empty.
-
AuthorPosts
-
February 19, 2026 at 5:52 am #6106
When learning Django, many beginners ask an important question:
“Why do we write
views.addand notviews.add()inurls.py?”At first, this looks confusing. After all, in Python, we usually use
()to call functions.So why does Django do it differently?
Let’s understand this step by step in simple terms.
📌 Understanding the Problem
Suppose you have this function in
views.py:def add(request): return render(request, "tasks/add.html")And in
urls.py, you write:path("add/", views.add, name="add")Many learners wonder:
❓ Why not write
views.add()instead?
✅ Function Name vs Function Call
In Python, there is a big difference between:
1️⃣ Function Reference (No Brackets)
addThis means:
“Here is the function. I’m not running it yet.”
2️⃣ Function Call (With Brackets)
add()This means:
“Run this function now and return the result.”
Example:
def hello(): return "Hi" print(hello) # Function reference print(hello()) # Function callOutput:
<function hello at 0x...> HiSo:
Code Meaning hello The function itself hello() Runs the function
📌 What Django Needs in
urls.pyWhen Django reads
urls.py, it is not ready to handle requests yet.It is just preparing routes.
So when you write:
path("add/", views.add)You are telling Django:
“Here is the function. Call it later when someone visits this URL.”
Django will store this function and run it when needed.
🚫 What Happens If You Write
views.add()?If you write:
path("add/", views.add(), name="add") # WRONGPython will immediately try to run:
add()But your function is:
def add(request):It needs a
requestargument.At startup, Django doesn’t have that yet.
So you’ll get an error like:
TypeError: add() missing 1 required argument💥 Your app may crash before running.
📞 Real-Life Analogy: Phone Number vs Calling
Think of it like this:
✔ Giving a Phone Number
views.add= “Here is the number. Call it when needed.”
❌ Calling Immediately
views.add()= “Call now, even though nobody asked.”
Django wants the number, not the call.
🔁 Callback Functions (Important Concept)
In programming, this idea is called a callback function.
A callback is:
A function you give to another system so it can run it later.
Example:
path("add/", views.add)Here,
views.addis a callback.Django calls it when a user visits the URL.
💡 Similar Example in JavaScript
If you know JavaScript:
button.onclick = sayHello; // Correct button.onclick = sayHello(); // WrongSame concept.
You give the function, not its result.
🧠 Why Django Calls It For You
When a user visits:
/tasks/add/Django internally does:
views.add(request)It creates the
requestobject and passes it to your function.That’s why you don’t need
().
⚠️ Golden Rule for Beginners
📌 In
urls.py, never use()with view functions.Always write:
views.addNot:
views.add()
✅ Summary Table
Code Meaning Correct? views.add Give function to Django ✅ Yes views.add() Run function now ❌ No
📘 Final Takeaway
When you write:
path("add/", views.add)You are saying:
“Django, here is my function.
Please call it when someone visits this URL.”Not:
“Run this function right now.”
Understanding this concept will help you not only in Django, but also in advanced Python programming.
-
AuthorPosts
- You must be logged in to reply to this topic.

