› Forums › Web Development › HarvardX: CS50W – CS50’s Web Programming with Python and JavaScript › CS50W – Lecture 3 – Django › Django Learning Tutorial: Where to Edit `views.py` and `urls.py` (Project vs App Explained)
- This topic is empty.
-
AuthorPosts
-
February 18, 2026 at 9:31 am #6096
When learning Django, one of the most confusing questions for beginners is:
❓ “Should I edit
views.pyandurls.pyin the main project folder or inside my app?”In this tutorial, you will learn:
✅ The difference between Project and App
✅ Whereviews.pyshould go
✅ Whereurls.pyshould go
✅ How everything connects togetherBy the end, this confusion will be completely clear.
🔹 Step 1: Understanding Django’s Two Main Parts
Every Django website has two main layers:
1️⃣ Project (Main Controller)
- Controls the whole website
- Handles settings and main routing
- Created using
startproject
2️⃣ App (Feature Module)
- Handles specific features (tasks, blog, users, etc.)
- Created using
startapp
Think of it like this:
Part Example Project The whole company App One department (Tasks, Blog, Users)
🔹 Step 2: Typical Django Folder Structure
After creating a project and app, your folders usually look like this:
myproject/ │ ├── myproject/ ← Main Project Folder │ ├── settings.py │ ├── urls.py ← Project URLs │ └── wsgi.py │ ├── tasks/ ← App Folder │ ├── views.py ← App Views │ ├── models.py │ ├── urls.py ← App URLs (you create this) │ └── templates/ │ └── manage.pyThis structure is very important in Django.
🔹 Step 3: Where Do We Write
views.pyCode?When you add this function:
def add(request): return render(request, "tasks/add.html")👉 It must go inside:
tasks/views.py✅ Correct Location
# tasks/views.py from django.shortcuts import render def add(request): return render(request, "tasks/add.html")❌ Wrong Location
Do NOT put this in:
myproject/views.py ❌(Usually, this file doesn’t even exist.)
📌 Why in the App?
Because:
- Views belong to features
- Each app manages its own logic
- Keeps code clean and organized
So:
👉 Task logic →
tasks/views.py
🔹 Step 4: Where Do We Write
path()inurls.py?Django uses two levels of URL files:
Level File Purpose Project myproject/urls.pyMain router App tasks/urls.pyFeature routes
🔹 Step 4A: Create
tasks/urls.pyInside the
tasksfolder, create:tasks/urls.pyThen add:
from django.urls import path from . import views urlpatterns = [ path("add", views.add, name="add"), ]This is where:
path("add", views.add, name="add")belongs.
🔹 Step 4B: Connect App URLs to Project URLs
Now open:
myproject/urls.pyEdit it like this:
from django.contrib import admin from django.urls import path, include urlpatterns = [ path("admin/", admin.site.urls), path("", include("tasks.urls")), ]This line is very important:
include("tasks.urls")It tells Django:
👉 “Load URLs from the tasks app.”
🔹 Step 5: How Django Handles a Request
When a user visits:
http://127.0.0.1:8000/addDjango works like this:
Browser ↓ Project urls.py ↓ include(tasks.urls) ↓ App urls.py ↓ views.add ↓ add.htmlSo:
1️⃣ Project receives request
2️⃣ Sends it to app
3️⃣ App finds view
4️⃣ View shows template
🔹 Step 6: Simple Rule to Remember
Memorize this rule:
✅ Views → Always in App
✅ URL paths → In App
✅ Main URL file → Connects AppsFile Location views.pyInside app folder path()Inside app urls.pyinclude()Inside project urls.py
🔹 Step 7: Common Beginner Mistake
Many beginners write everything in:
myproject/urls.pyExample (Bad Practice):
path("add", views.add)This works, but:
❌ Makes project messy
❌ Hard to scale
❌ Breaks modular designDjango is built for modular apps, so each app should manage its own URLs.
🔹 Step 8: Why Django Uses This Design
Django uses this structure to help you:
✔ Reuse apps in other projects
✔ Manage big websites easily
✔ Avoid mixing unrelated code
✔ Work in teams betterProfessional Django projects always follow this pattern.
🏁 Final Summary
Let’s answer the original question clearly:
❓ Are we updating
views.pyin the main project or app?✅ Answer:
Item Location def add()tasks/views.pypath("add")tasks/urls.pyinclude()myproject/urls.py
In One Line:
👉 Write logic in the app.
👉 Route it in the app.
👉 Connect everything in the project.
-
AuthorPosts
- You must be logged in to reply to this topic.

