Django URL patterns | Python Read Courses Practice Improve Improve Improve Like Article Like Save Article Save Report issue Report Prerequisites: Views in Django In Django, views are Python functions which take a URL request as parameter and return an HTTP response or throw an exception like 404. Each view needs to be mapped to a corresponding URL pattern. This is done via a Python module called URLConf(URL configuration) Let the project name be myProject. The Python module to be used as URLConf is the value of ROOT_URLCONF in myProject/settings.py. By default this is set to 'myProject.urls'. Every URLConf module must contain a variable urlpatterns which is a set of URL patterns to be matched against the requested URL. These patterns will be checked in sequence, until the first match is found. Then the view corresponding to the first match is invoked. If no URL pattern matches, Django invokes an appropriate error handling view. Including other URLConf modules It is a good practice to have a URLConf module for every app in Django. This module needs to be included in the root URLConf module as follows: from django.contrib import admin from django.urls import path, include urlpatterns = [ path('admin/', admin.site.urls), path('', include('books.urls')), ] This tells Django to search for URL patterns in the file books/urls.py. URL patterns Here’s a sample code for books/urls.py: from django.urls import path from . import views urlpatterns = [ path('books/<int:pk>/', views.book_detail), path('books/<str:genre>/', views.books_by_genre), path('books/', views.book_index), ] For example, A URL request to /books/crime/ will match with the second URL pattern. As a result, Django will call the function views.books_by_genre(request, genre = "crime"). Similarly a URL request /books/25/ will match the first URL pattern and Django will call the function views.book_detail(request, pk =25). Here, int and str are path convertors and capture an integer and string value respectively. Path convertors: The following path convertor types are available in Django int – Matches zero or any positive integer. str – Matches any non-empty string, excluding the path separator(‘/’). slug – Matches any slug string, i.e. a string consisting of alphabets, digits, hyphen and under score. path – Matches any non-empty string including the path separator(‘/’) uuid – Matches a UUID(universal unique identifier). Don't miss your chance to ride the wave of the data revolution! Every industry is scaling new heights by tapping into the power of data. Sharpen your skills, become a part of the hottest trend in the 21st century.Dive into the future of technology - explore the Complete Machine Learning and Data Science Program by GeeksforGeeks and stay ahead of the curve. Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore. Last Updated : 19 Jul, 2019 Like Article Save Article Previous Render Django Form Fields Manually Next Get parameters passed by urls in Django Share your thoughts in the comments Add Your Comment Please Login to comment...