In this tutorial, we will learn about URL routing in Django using the path()
function. We will understand how Django uses this function to map URLs to views, and how to configure it for our application.
By the end of this tutorial, you will be able to configure URL routing for your Django application using the path()
function.
You should have Python and Django installed on your machine. Basic familiarity with Django views and URLs is also recommended.
In Django, when a user makes a request to a URL, Django uses URL routing to decide which view to call. This is done by comparing the requested URL against a list of URL patterns defined in the application. The URL patterns are defined using the path()
function in your application's urls.py
file.
The path()
function is imported from django.urls
and it takes two required arguments: the route and the view, and two optional arguments: kwargs, and name.
route
: This is a string that contains a URL pattern. When processing a request, Django starts at the first pattern in the list and makes its way down the list until it finds a pattern that matches the requested URL.view
: This is a view function which Django calls when it finds a matching pattern.kwargs
: Arbitrary keyword arguments can be passed in a dictionary to the target view.name
: Naming your URL lets you refer to it unambiguously from elsewhere in Django.name
parameter for your URLs. It allows you to change the URL in one place and not have to remember to change it in every place it is used.# Import the path function
from django.urls import path
# Import your view
from . import views
# Define your URL patterns
urlpatterns = [
# When the user requests the root URL, call the home function in views
path('', views.home, name='home'),
]
In the example above, ''
is the route, views.home
is the view, and 'home'
is the name of the URL pattern.
urlpatterns = [
# When the user requests /user/<username>, call the profile function in views
path('user/<str:username>', views.profile, name='profile'),
]
In the example above, <str:username>
is a URL parameter. str
means that we expect a string, and username
is the variable name that will be passed to the view. In our view, we can access it like this:
def profile(request, username):
# Now you can use the username variable
print(username)
In this tutorial, we learned about URL routing in Django and how to use the path()
function to define URL patterns. We also learned how to pass parameters from the URL to our views.
Create a URL pattern for a page that displays a blog post by its ID. The ID is an integer.
path('post/<int:post_id>', views.post_detail, name='post_detail'),
In this solution, <int:post_id>
is a URL parameter. int
means that we expect an integer, and post_id
is the variable name that will be passed to the view.
Create a URL pattern for the route /products/<category>/<product_id>/
, where category
is a string and product_id
is an integer.
path('products/<str:category>/<int:product_id>/', views.product_detail, name='product_detail'),
In this solution, we have two URL parameters: <str:category>
and <int:product_id>
. In our view, we can access them like this:
def product_detail(request, category, product_id):
# Now you can use the category and product_id variables
print(category, product_id)
Keep practicing and experimenting with various routes and views. Happy coding!