› Forums › Web Development › HarvardX: CS50W – CS50’s Web Programming with Python and JavaScript › CS50W – Lecture 3 – Django › Why Does Django Have Two urls.py Files?
- This topic is empty.
-
AuthorPosts
-
April 24, 2026 at 10:24 am #6452
When beginners open a Django project, they often notice something interesting:
There may be one
urls.pyinside the main project folder (likewiki) and another inside the app folder (likeencyclopedia).At first, this can feel confusing.
Why two files doing the same thing?
Short Answer
They have different jobs:
- Project
urls.py= controls the whole website - App
urls.py= controls only that specific app
So Django splits responsibilities for better organization.
1. Main Project URLs
Inside the
wikifolder:from django.contrib import admin from django.urls import include, path urlpatterns = [ path('admin/', admin.site.urls), path('', include("encyclopedia.urls")) ]This means:
/admin/goes to Django admin panel/sends traffic to the encyclopedia app
Think of this file as the main traffic controller.
2. App URLs
Inside the
encyclopediafolder:from django.urls import path from . import views urlpatterns = [ path("", views.index, name="index") ]This means:
- When someone visits the homepage
- Run
views.index
This file is shorter because it only manages one app.
How a Request Travels
If a user visits:
http://site.com/Django does this:
- Checks project
urls.py - Finds:
path('', include("encyclopedia.urls")) - Then checks app
urls.py - Finds:
path("", views.index) - Runs the
indexview
Why Django Uses This System
Because large websites need clean structure.
Later, you may add more apps:
urlpatterns = [ path('', include("encyclopedia.urls")), path('accounts/', include("accounts.urls")), path('shop/', include("shop.urls")), ]Each app keeps its own routes.
That makes projects easier to maintain.
Simple Analogy
Think of a shopping mall:
- Main directory at entrance = project
urls.py - Food court internal map = app
urls.py
One routes visitors globally.
One routes visitors locally.
Final Insight
Two
urls.pyfiles are not duplication.They are Django’s way of keeping projects modular, scalable, and professional.
- Project
-
AuthorPosts
- You must be logged in to reply to this topic.
