7. Django URLs
URL dispatching in Django is the process by which HTTP requests for resources are mapped to functions in Django views.
You may also find such a process referred to as routing as this terminology is used in other web-application frameworks, such as Ruby on Rails.
In the typical layout of a Django application, URL dispatch happens across one or more URLs files. Conventionally, there is a main URLs file for the whole Django project and specific URLs files for each application.
When a web server receives a resource requests for your application, it will pass the resource path information to the application. The application establishes, reaches the main project URLs file and runs the path resolution code in that file.
The location of the main project URLs file is controlled in the Django settings. But by convention, this file can be found in the configuration and settings directory for the project. If we turn to the file browser, if we open the project, and then open the directory, we should find urls.py.
The typical use case for the project URLs file is to include other URLs files in the various sub apps. The logic here is that the project URLs file is responsible for deciding which app a resource request is sent to you. Then the URL dispatch within the app maps the specific functions in the view.
A URLs file is a Python script and in most cases, it is used simply to build the contents of a static list called URLpatterns.
from django.urls import path, re_path from . import views urlpatterns = [ path('simple', views.simple_view, name='simple'), ]
Wednesday 20 October 2021, 117 views
Next post: 8. Building a lightweight project
Previous post: 6. Django templates
Advanced Web Development index
- 38. Writing API tests
- 37. Testing in Django
- 36. Class-based views in the Django REST framework
- 35. Building a RESTful web service in Django
- 34. Introduction to CRUD, REST and APIs
- 33. Refactoring with generic views in Django
- 32. Django validators
- 31. Django forms (2) – using the ModelForm class
- 30. Django forms (1)
- 28. Adding CSS to the template
- 27. Django templating
- 26. Deleting and updating records
- 25. Joins, filters and chaining commands
- 24. Using the ORM in views.py
- 23. Adding to the database by writing a script
- 22. Adding to the database with Django Admin
- 21. Migrations
- 20. ORM – work through example
- 19. An introduction to the Object-Relational Mapper
- 18. Altering the database
- 17. SQL functions and summaries
- 16. SQL Query performance
- 15. Queries and table joins in SQL
- 14. Inserts and queries in SQL
- 13. Good practice in relational database design
- 12. Limitations to database modelling
- 11. Building a database using SQL
- 10. Introduction to PostgreSQL
- 9. How to start writing a new application in Django
- 8. Building a lightweight project
- 7. Django URLs
- 6. Django templates
- 5. Django models
- 4. Django views
- 3. Creating a new hello app
- 2. Creating a new virtual environment
- 1. Setting up Django