Open In App

Get the Current URL within a Django Template

Last Updated : 01 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Django, a high-level Python web framework, encourages rapid development and clean, pragmatic design. One common requirement when developing web applications is to access the current URL within a template. This can be useful for various purposes such as highlighting the active link in a navigation menu or generating dynamic content based on the URL.

In this article, we will walk you through the steps to create a Django project and demonstrate how to get the current URL within a Django template.

Here are a few useful attributes of the request object that we will use:

  • {{ request.path }}: The path of the current URL without the domain.
  • {{ request.build_absolute_uri }}: The full URL including the domain.

Get the Current URL within a Django Template

Before we dive into fetching the current URL within a template, let's first set up a basic Django project. If you haven't installed Django yet, you can do so by running

pip install django

Step 1: Start a New Django Project

Open your terminal and run the following command to start a new Django project named myproject:

django-admin startproject myproject

Navigate into the project directory:

cd myproject

Step 2: Create a Django App

Next, create a new app within the project. We'll name it myapp:

python manage.py startapp myapp

Add the new app to the INSTALLED_APPS list in myproject/settings.py:

INSTALLED_APPS = [
...
'myapp',
]
f1


Step 3: Create a View

In myapp/views.py, create a simple view for the home page and about page:

Python
from django.shortcuts import render

def home(request):
    return render(request, 'home.html')

def about(request):
    return render(request, 'about.html')

Step 4: Create a Template

Create a directory named templates within the myapp directory, and inside it, create a file named home.html and about.html. In the example above, we use {{ request.get_full_path }} within the home.html template. This expression will output the current URL, including the query string if any.

home.html

HTML
<!DOCTYPE html>
<html>
<head>
    <title>Home Page</title>
</head>
<body>
    <h1>Welcome to MyApp</h1>
    <p>The current URL is: {{ request.get_full_path }}</p>
    <a href="{% url 'about' %}">About</a>
</body>
</html>

about.html

HTML
<!DOCTYPE html>
<html>
<head>
    <title>About Page</title>
</head>
<body>
    <h1>About MyApp</h1>
    <p>The current URL is: {{ request.get_full_path }}</p>
    <a href="{% url 'home' %}">Home</a>
</body>
</html>

Step 5: Define a URL Pattern

Open myproject/urls.py and include the URL patterns for myapp:

Python
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('myapp.urls')),
]

Now, create a urls.py file within the myapp directory and define a simple URL pattern:

Python
from django.urls import path
from . import views

urlpatterns = [
    path('', views.home, name='home'),
    path('about/', views.about, name='about'),
]

Run the Server

run the server using the below command

python manage.py runserver

navigate the http://127.0.0.1:8000/ in web browser

Output

2
1

Conclusion

Accessing the current URL within a Django template is straightforward once you have the request object available. By configuring the request context processor, you can easily retrieve various attributes of the current request, including the URL, and use them within your templates to create dynamic and responsive web applications.


Next Article
Practice Tags :

Similar Reads