URL Configuration
Django views cannot work on their own in a web application. When a web request is made to the application, Django's URL configuration takes care of routing the request to the appropriate view function to process the request. A typical URL configuration in the urls.py
file in Django looks like this:
from . import views urlpatterns = [path('url-path/' views.my_view, name='my-view'),]
Here, urlpatterns
is the variable defining the list of URL paths, and 'url-path/'
defines the path to match.
views.my_view
is the view function to invoke when there is a URL match, and name='my-view'
is the name of the view function used to refer to the view. There may be a situation wherein, elsewhere in the application, we want to get the URL of this view. We wouldn't want to hardcode the value, as it would then have to be specified twice in the codebase. Instead, we can access the URL by using the name of the view, as follows...