Python

Get the Current URL in the Django Template

Django template is used to display the application data for the users. Sometimes it retrieving and displaying of the current URL in the template. The current URL can be displayed in different ways, such as only the domain name, a domain name with path, and the domain name with path and http. How the current URL can be displayed in the Django template in different formats will be shown in this tutorial.

Prerequisites:

Before practicing the script of this tutorial, you must complete the following tasks;

  1. Install the Django version 3+ on Ubuntu 20+ (preferably)
  2. Create a Django project
  3. Run the Django server to check the server is working properly or not.

Setup a Django App:

1. Run the following command to create a Django app named geturlapp.

$ python3 manage.py startapp geturlapp

2. Run the following command to create the user that will be used to access the Django database. If you have created the user before then you don’t need to run the command.

$ python3 manage.py createsuperuser

3. Add the app name in the INSTALLED_APP part of the settings.py file.

INSTALLED_APPS = [
    …..
    'geturlapp'
]

4. Create a folder named templates inside the geturlapp folder and set the template’s location of the app in the TEMPLATES part of the settings.py file.

TEMPLATES = [
    {
….
            'DIRS': ['/home/fahmida/django_pro/validationapp/templates'],
             ….
      },
]

Create and Modify the Necessary Files:

Three different HTML files were created in this part of this tutorial to display the current URL in three different formats.

Create the index.html file with the following script to display the domain name of the current URL only.

index.html

<head>
    <title>Read Domain Name</title>
</head>
<body>
<center><h1 style="color:blue">The domain name of the current URL = {{ showURL }} </h1></center>
</body>
</html>

Create the index2.html file with the following script to display the domain name with the path of the current URL.

index2.html

<head>
    <title>Read Current URL</title>
</head>
<body>>
<center><h2 style="color:green">The domain name of the current URL = {{ showURL }} </h2></center>
</body>
</html>

Create the index3.html file with the following script to display the domain name with the path and http of the current URL.

index3.html

<head>
    <title>Read Current URL with HTTP</title>
</head>
<body>
<center><h3 style="color:red">The domain name of the current URL = {{ showURL }} </h3></center>
</body>
</html>

Modify the views.py file of the geturlapp folder with the following script. Three functions had been defined in the script to return the current URL value in three different formats to the template. The geturl1() function has been defined to retrieve the domain name of the current URL and send it to the index.html file. Request.get_host() function has been used to retrieve the domain name of the current URL. The geturl2() function has been defined to retrieve the domain name with the path of the current URL and send it to the index2.html file. On the other hand, request.path attribute has been used with the request.get_host() function to read the path with the domain name of the current URL. The geturl3() function has been defined to retrieve the domain name with the http and the path of the current URL and send it to the index3.html file. request._current_scheme_host attribute has been used to retrieve the domain name with http. The return value of each function will be passed to the template using the showURL tag.

views.py

# Import render module
from django.shortcuts import render

# Read only the domain name of the current URL
def geturl1(request):
    urlObject = request.get_host()
    return render(request, 'index.html', {'showURL': urlObject})

# Read the domain name with the path
def geturl2(request):
    urlObject = request.get_host() + request.path
    return render(request, 'index2.html', {'showURL': urlObject})

# Read the domain name with the http and path
def geturl3(request):
    urlObject = request._current_scheme_host + request.path
    return render(request, 'index3.html', {'showURL': urlObject})

Modify the urls.py file of the geturlapp folder with the following script. Three paths had been defined in the script for accessing the three functions of the view file. The empty string(”) path will be used to call the geturl1() function. The ‘index2’ path will be used to call the geturl2() function. The ‘index3’ path will be used to call the geturl3() function.

urls.py

# Import path module
from django.urls import path
# Import view
from geturlapp import views

# Define paths to read current URL
urlpatterns = [
    # Display the domain name in the template
    path('', views.geturl1),
    # Display the domain name with path in the template
    path('index2', views.geturl2),
    # Display the domain name with http and path in the template
    path('index3', views.geturl3),

]

Output:

Run the following command to start the Django server.

$ python3 manage.py runserver

Execute the following URL from the browser to display the domain name of the current URL. The geturl1() function will be called for this URL that will send the domain name to the index.html file.

http://localhost:8000

The following output will appear after executing the script.

Execute the following URL from the browser to display the domain name with the path of the current URL. The geturl2() function will be called for this URL that will send the domain name with the path to the index2.html file.

http://localhost:8000/index2

The following output will appear after executing the script. Here, the path is index2.

Execute the following URL from the browser to display the domain name with the http and the path of the current URL. The geturl3() function will be called for this URL that will send the domain name with the http and the path to the index3.html file.

http://localhost:8000/index3

The following output will appear after executing the script. Here, the path is index3.

Conclusion:

The current URL can be displayed in the Django template using the method and the attributes of the request object. Three templates had been created in this tutorial to display the current URL in three different ways that will help the readers to know the way of reading the current URL in the Django template.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.