The path function helps Django know which code to run when someone visits a web address on your site.
path function for routes in Django
path(route, view, name=None)route is the URL pattern as a string, like 'home/' or 'blog/
view is the function or class that runs when the URL is visited.
name is an optional label to refer to this route elsewhere.
home_view function and names it 'home'.path('home/', views.home_view, name='home')
id to the view.path('article/<int:id>/', views.article_detail, name='article-detail')
index view.path('', views.index, name='index')
This example shows three routes: the home page, an about page, and a product detail page that takes a product ID from the URL.
from django.urls import path from . import views urlpatterns = [ path('', views.home, name='home'), path('about/', views.about, name='about'), path('product/<int:id>/', views.product_detail, name='product-detail'), ] # views.py from django.http import HttpResponse def home(request): return HttpResponse('Welcome to the Home Page') def about(request): return HttpResponse('About Us Page') def product_detail(request, id): return HttpResponse(f'Product Details for product id: {id}')
Always end your URL patterns with a trailing slash for consistency.
Use name to easily refer to URLs in templates and redirects.
Dynamic parts like <int:id> let you capture values from the URL.
The path function connects URLs to code that runs when those URLs are visited.
You can use simple strings or dynamic parts in the URL pattern.
Giving routes a name helps you manage URLs easily in your project.
Practice
path function in Django?Solution
Step 1: Understand the role of URL routing
Django uses URL routing to decide which code runs when a user visits a URL.Step 2: Identify what
Thepathdoespathfunction links a URL pattern to a view function or class that handles the request.Final Answer:
To connect a URL pattern to a view function or class -> Option BQuick Check:
URL routing = connect URL to view [OK]
- Confusing path with database models
- Thinking path styles HTML
- Assuming path runs background tasks
path in Django?Solution
Step 1: Recall the
Thepathfunction signaturepathfunction takes at least two arguments: the URL pattern string and the view function. Optionally, anameargument can be added.Step 2: Check each option's syntax
path('home/' views.home_view') misses comma between arguments, invalid syntax. path = 'home/' views.home_view is invalid syntax. path('home/', views.home_view, name='home') correctly usespath('home/', views.home_view, name='home'). path('home', views.home_view, 'home') has incorrect argument order and missing keyword.Final Answer:
path('home/', views.home_view, name='home') -> Option AQuick Check:
Correct syntax includes URL, view, and optional name [OK]
- Omitting commas between arguments
- Missing quotes around URL string
- Passing name without keyword
path('article//', views.article_detail, name='article_detail')What URL will match this pattern?
Solution
Step 1: Understand the dynamic part in the URL pattern
The pattern uses<int:id>which means it expects an integer value in that part of the URL.Step 2: Match each option to the pattern
/article/42/ has an integer 42 and ends with a slash, matching the pattern exactly. /article/abc/ has letters 'abc' which is not an integer. /article/42 misses the trailing slash. /article// has no value where integer is expected.Final Answer:
/article/42/ -> Option AQuick Check:
Integer in URL with trailing slash matches [OK]
- Ignoring trailing slash
- Using wrong data type in URL
- Leaving dynamic part empty
path('profile/<username>/', views.profile_view, name='profile')Solution
Step 1: Check the dynamic part syntax
Django requires a type converter likestrorintinside angle brackets for dynamic parts, e.g.,<str:username>.Step 2: Analyze the given pattern
The pattern uses<username>without a type converter, which is invalid syntax.Final Answer:
Missing type converter for <username> -> Option CQuick Check:
Dynamic parts need type like str or int [OK]
- Leaving out type converter in dynamic URL parts
- Forgetting commas between arguments
- Assuming any string works as dynamic part
/blog/2024/06/ where the year and month are dynamic integers. Which of the following path definitions is correct?Solution
Step 1: Understand the correct syntax for dynamic URL parts
The correct format is<type:variable>, for example<int:year>.Step 2: Evaluate each option
path('blog///', views.blog_archive, name='blog_archive') uses correct syntax for both year and month as integers. path('blog///', views.blog_archive, name='blog_archive') reverses type and variable order, which is invalid. path('blog//<month>/', views.blog_archive, name='blog_archive') misses type for month. path('blog/<year>/<month>/', views.blog_archive, name='blog_archive') misses types for both year and month.Final Answer:
path('blog/<int:year>/<int:month>/', views.blog_archive, name='blog_archive') -> Option DQuick Check:
Use <int:var> for integer dynamic parts [OK]
- Swapping type and variable order
- Omitting type converters
- Using unknown types
Ready to prove it?
Pass the test and this pattern moves unknown
