Projects and Apps
Part of the Django Basics section of Coddy's Django journey. Lesson 4 of 23.
The menu app has a new Opening Hours page in menu/urls.py, but /hours/ shows Page not found, because the project never reads that file.
A Django project is the whole site: mysite/ holds its settings and its main URLconf. An app is one part of the site with its own views and URLs, here menu/. On your computer, python manage.py startapp menu makes one.
The project hands an app part of the address with include():
from django.urls import include, path
urlpatterns = [
path("shop/", include("shop.urls")),
]Django matches the start of the address, shop/, then hands the rest to shop/urls.py. Its "cart/" route is then at /shop/cart/. The prefix can be any text, even empty.
Challenge
EasyMake the project hand its addresses to the menu app.
- In
mysite/urls.py, importincludenext topath. - Replace the two routes with one route that includes
menu.urls. Give it an empty prefix,"", so/and/about/stay where they are.
Open /hours/ and /about/: both still work.
Try it yourself
from django.urls import path
from menu import views
urlpatterns = [
path("", views.home),
path("about/", views.about),
]
All lessons in Django Basics
Practice on your own: Django playground