0
0
Djangoframework~30 mins

URL parameters with angle brackets in Django - Mini Project: Build & Apply

Choose your learning style9 modes available
URL Parameters with Angle Brackets in Django
📖 Scenario: You are building a simple Django web app that shows details for different products. Each product has a unique ID number. You want to create a URL pattern that captures this product ID from the URL using angle brackets.
🎯 Goal: Build a Django URL pattern that uses angle brackets to capture a product ID as a URL parameter, and create a simple view function that receives this parameter and returns a response showing the product ID.
📋 What You'll Learn
Create a URL pattern in urls.py using angle brackets to capture an integer parameter called product_id
Create a view function called product_detail in views.py that accepts product_id as an argument
Return an HTTP response from the view that includes the product_id in the text
Connect the URL pattern to the product_detail view
💡 Why This Matters
🌍 Real World
Capturing URL parameters is essential for building dynamic web pages that show different content based on user input or links.
💼 Career
Understanding Django URL routing and parameter capturing is a key skill for backend web developers working with Django.
Progress0 / 4 steps
1
Create the URL pattern dictionary
In urls.py, create a list called urlpatterns with one entry: a call to path with the route '' (empty string), the view product_detail, and the name 'product_detail'. Import path from django.urls and product_detail from views.
Django
Need a hint?

Remember to import path and your view function before creating urlpatterns.

2
Add the URL parameter with angle brackets
Modify the path in urlpatterns to capture an integer parameter called product_id using angle brackets. Change the route from '' to 'product//'.
Django
Need a hint?

Use <int:product_id> inside the route string to capture the integer parameter.

3
Create the view function to accept the parameter
In views.py, define a function called product_detail that takes request and product_id as parameters. Return an HttpResponse with the text "Product ID: {product_id}" using an f-string. Import HttpResponse from django.http.
Django
Need a hint?

Make sure your function accepts product_id and uses it inside the response text.

4
Connect the URL pattern and view
Ensure that urls.py imports product_detail from views and that the urlpatterns list includes the path 'product//' connected to product_detail. This completes the URL parameter setup.
Django
Need a hint?

Double-check your imports and that the URL pattern matches the view function.