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
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:
This tells Django to search for URL patterns in the file
Here’s a sample code for books/urls.py:
- 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).
str are path convertors and capture an integer and string value respectively.
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).
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Connect Django Project to MongoDB using Django
- Styling Django Forms with django-crispy-forms
- Adding Tags Using Django-Taggit in Django Project
- url - Django Template Tag
- URL fields in serializers - Django REST Framework
- Programs for printing pyramid patterns in Python
- Find all the patterns of “1(0+)1” in a given string using Python Regex
- Factory Method - Python Design Patterns
- Observer method - Python Design Patterns
- Composite Method - Python Design Patterns
- Decorator Method - Python Design Patterns
- Abstract Factory Method - Python Design Patterns
- Builder Method - Python Design Patterns
- Singleton Method - Python Design Patterns
- Adapter Method - Python Design Patterns
- Prototype Method - Python Design Patterns
- Bridge Method - Python Design Patterns
- Facade Method - Python Design Patterns
- Proxy Method - Python Design Patterns
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.