Django URLs

Learn via video courses
Topics Covered

Overview

URL(Uniform resource locator) is a web address that we use to see on our browser address bar whenever we visit any website. Every page on the Internet has a unique URL, and we access the page by that URL. Django uses URLconf (URL configuration) to handle the web request. URLconf is a set of patterns that Django tries to match with the requested URL to find the correct view. A view in Django is a Python function that accepts a Web request and delivers a Web response. In Django, a request is routed through urls.py and then to the corresponding function in views.py. views.py routines take the web request from urls.py and return the web result to templates.

Introduction

Django is open source python framework, and it follows the MVT structure. In MVT, M stands for Model, which is used to create table and their fields. V stands for views, a Python function that accepts a Web request and delivers a Web response.T stands for Templates containing the static content of a Django project like Html, CSS, and Javascript, along with the image used in the project. django mvt

People using other frameworks based on MVC (Model, View, Controller) structure often confuse Django views with different framework views. But Django views are associated with controllers in other frameworks, and templates in Django are related to views in other frameworks.

Django URLs Path

The Django path is included in the Django project code base as part of the django.urls module. path is used for routing URLs to the appropriate view functions within a Django application using the URL dispatcher.

Syntax:

  • The route argument should be a string with a URL pattern in it.
  • The view argument is a view function written in view.py file.
  • You can pass additional parameters to the view function or method using the kwargs argument.
  • In order to perform URL reversing, you’ll need to use named URL patterns.

Django project contains all the URL paths in the urls.py file. Multiple urls.py files are possible, in a single Django project. The one urls.py file is auto-generated with the project creation, i.e., the main project urls.py file. It also has a pre-defined path for the admin app. The example of the default urls.py file is mentioned below:

Django URLs Mapping

Mapping of URL refers to connecting a user request with a Django view. To map a URL, first, we have to map the Django app URL in the project's urls.py file. We have used include(), which is a python function that adds URLs from your app directory's urls.py to the main urls.py (in memory). This keeps the main urls.py from getting too big to read.

In the above code, we connected the Django app URLs to the main project URLs. This means all the requests with /app will redirect to the app urls.py file.Now we can declare all the URL mapping inside <app>/urls.py file:

Now URL request to /app/home/ will match with the URL pattern. As a result, Django will call the function views.home.

Django URLs Functions

Django URLs function handles the user request and maps the request to respective views. Let's discuss the important django.urls function in detail:

Function NameSyntax
path()path(route, view, kwargs=None, name=None)
re_path()re_path(route, view, kwargs=None, name=None)
include()include(module, namespace=None) ,include(pattern_list)
register_converter()register_converter(converter, type_name)

path()

The Django path is included in the Django project code base as part of the django.urls module. path is used for routing URLs to the appropriate view functions within a Django application using the URL dispatcher.

Syntax:

  • The route argument should be a string with a URL pattern in it.
  • The view argument is a view function written in view.py file.
  • You can pass additional parameters to the view function or method using the kwargs argument.
  • In order to perform URL reversing, you’ll need to use named URL patterns.

Example:

re_path()

Syntax:

  • Returns an element for inclusion in urlpatterns.
    • The route argument should be a string with a URL pattern in it.
  • The view argument is a view function written in view.py file.
  • You can pass additional parameters to the view function or method using the kwargs argument.
  • In order to perform URL reversing, you’ll need to use named URL patterns.

Example:

include()

Syntax:

  • module – URLconf module (or module name)
  • namespace (str) – Instance namespace for the URL entries being included
  • pattern_list – Iterable of path() and/or re_path() instances.
  • app_namespace (str) – Application namespace for the URL entries being included

register_converter()

Syntax:

  • The function for registering a converter for use in path() routes.
  • The converter argument is a converter class, and type_name is the converter name to use in path patterns.

Organizing Your URLs

It's essential to have an organized URL in a project because a project contains many URL patterns, and it will be tough to work with an unorganized URL. We can create a separate urls.py file for each app to organize URLs better. Let's see through an example:

The Project(mysite) urls.py file.

The App(firstapp) urls.py file

Organizing Your URLs

In the above example, you can see that we have added a separate urls.py file for an app name "firstapp". So it will be easier to organize the other URL in the future. We can also create another app and create a separate urls.py file for that app.

Sending Parameters to Views

For capturing part of a URL to be used in a view function, you can do it by configuring your URLs. For example, if the URL is

Then product will be the URL endpoint, and 12 will be the URL parameter. For capturing the URL parameter edit <app>/urls.py file as shown below:

In the above code, I have created a path to the product endpoint and defined the path converter as int to specify that the URL parameter will be an integer. Now, pass the same productId as the argument in the view for capturing that URL parameter in view.

Sending Parameters to Views

Conclusion

  • URL(Uniform resource locator) is a web address we will see on our browser address bar whenever we visit any website.
  • Django is open source python framework, and it follows the MVT structure.
  • Django path is used for routing URLs to the appropriate view functions within a Django application using the URL dispatcher.
  • Django URL function handles the user request and maps the request to respective views.