Django URLs detailed explanation
How does Django know which view to show when receiving a petition? I reorganized the information, focus on the basics, and redact the documentation in a way easier to understand.
1. It determines the root module. Commonly it is in the project configuration folder, in the file urls.py
2. It searches the variable called urlpatterns.
3. It compares the required URL with each pattern described on urlpatterns and enters the first view that makes a match.
- It ignores the method of the request(GET, POST, etc), all petitions are routed to the first view that matches.
- If the urlpattern has include(), it cuts the URL and sends it to the referenced file to compare the routes described there.
4. The view receives
— The request
— Positional arguments
— Keyword arguments(parts of the URL)
5. If a coincidence is not found or there is found an exception, it generates errors 400, 403, 404, or 500.
Urlpatterns
It is a list of instances of path() or re_path() (import them from django.urls).
- path() allows:
— Include an entire file where there are more URLs to search on. It uses include() .
— Define a view to managing a request to a URL.
In this article, I will explain only the two cases of the path. - repath() allows the use of regular expressions to make the comparison.
These are some examples:
Ways to organize the urlpatterns
You want to have an organized system of files.
Especially in a Django project where you have multiple applications with many URLs (a big monorepo), and when some URLs share part of their routes.
The documentation describes these two options:
a. Include other URL files
You can create a urls.py file in each folder using include(). It adds all the urlpatterns found in the referenced application, in a urls.py file.
In this example, they have an app called community and contact. You will create a file called urls.py in the app folder. It will contain the views of each application.
In the app community, there will be a group of URLs that will start with the string `community/` plus the URL name given in the community URLs.
b. Agrupate in the same urls file
If you want to have the URLs in the same file, you can create a list of them with a descriptive name of the group and then include them in the urlpatterns like this:
The path to access to the credit reports will be created by adding the main string and the secondary: /credit/reports/
URL
¿Where do you write a backslash in the URLs?
- Don’t write a backslash at the beginning.
- Always write a backslash at the end.
Posicionals Arguments
You can capture a part of the URL to receive it as a parameter in the view using angular brackets, inside the URL: <name>
By default, without a converter, the parameter will be of string type.
Converters
You will need to use a converter to transform the parameter into an integer, string, slug, UUID, or path:
<int:year>
<str:name>
<slug:year>
<uuid:id>
Also, you can use customized converters.
Please let me know if this was useful to you with a like, or if you want more detailed explanations of Django. Thanks!
About me
I am a passionate Python Backend Developer with two years of experience creating APIs and backend of web applications. I am creating content that is useful to mentor others about the bases of software development and Django apps.
If you want to create a connection with me, follow me on GitHub, Twitter, or Linkedin.
I hope you enjoyed this reading!
Made by Natalia Vera Duran.