0
0
Djangoframework~30 mins

path function for routes in Django - Mini Project: Build & Apply

Choose your learning style9 modes available
Using the path function for routes in Django
📖 Scenario: You are building a simple Django web application for a bookstore. You want to set up URL routes so users can visit different pages like the homepage, a books list, and a contact page.
🎯 Goal: Learn how to use Django's path function to create URL routes that connect URLs to view functions.
📋 What You'll Learn
Create a list called urlpatterns to hold URL patterns
Use the path function to define routes
Map the route '' (empty string) to a view function named home_view
Map the route 'books/' to a view function named books_view
Map the route 'contact/' to a view function named contact_view
💡 Why This Matters
🌍 Real World
Setting up URL routes is essential in Django web apps to connect URLs to the code that handles requests.
💼 Career
Understanding Django routing is a key skill for backend web developers working with Python and Django frameworks.
Progress0 / 4 steps
1
Create the initial URL patterns list
Create an empty list called urlpatterns to hold your URL routes.
Django
Need a hint?

Think of urlpatterns as a list where you will add your routes.

2
Import the path function
Import the path function from django.urls to use for defining routes.
Django
Need a hint?

You need to import path before using it to define routes.

3
Add routes using the path function
Add three routes to urlpatterns using the path function: '' mapped to home_view, 'books/' mapped to books_view, and 'contact/' mapped to contact_view.
Django
Need a hint?

Use path(route, view_function) to add each route.

4
Complete with view function placeholders
Define simple placeholder view functions named home_view, books_view, and contact_view that accept a request parameter. These functions can just return None for now.
Django
Need a hint?

Each view function needs to accept request even if it does nothing yet.