the red penguin
HOME ABOUT SITEMAP BLOG LOGIN

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, 469 views


Leave a Reply

Your email address will not be published. Required fields are marked *