url – Django Template Tag

A Django template is a text document or a Python string marked-up using the Django template language. Django being a powerful Batteries included framework provides convenience to rendering data in a template. Django templates not only allow passing data from view to template, but also provides some limited features of programming such as variables, for loops, comments, extends, url, etc.
This article revolves about how to use url tag in Templates. url tag Returns an absolute path reference (a URL without the domain name) matching a given view and optional parameters. This is a way to output links without violating the DRY principle by having to hard-code URLs in your templates:

Syntax
{% url 'some-url-name' v1 v2 %}

The first argument is a URL pattern name. It can be a quoted literal or any other context variable. Additional arguments are optional and should be space-separated values that will be used as arguments in the URL.

Example
{% url 'template1' %}

url – Django template Tags Explanation

Illustration of How to use url tag in Django templates using an Example. Consider a project named geeksforgeeks having an app named geeks.

Refer to the following articles to check how to create a project and an app in Django.

Now create two views through which we will access the template,
In geeks/views.py,



filter_none

edit
close

play_arrow

link
brightness_4
code

# import Http Response from django
from django.shortcuts import render
   
# create a function
def geeks_view(request):
    # return response
    return render(request, "geeks.html")
  
def nav_view(request):
    # return response
    return render(request, "nav.html")

chevron_right


Create a url path to map to this view. URLs need to have a name which then can be used in templates and with url tag. In geeks/urls.py,

filter_none

edit
close

play_arrow

link
brightness_4
code

from django.urls import path
  
# importing views from views..py
from .views import geeks_view, nav_view
  
urlpatterns = [
    path('1/', geeks_view, name = "template1"),
    path('2/', nav_view, name = "template2"),
]

chevron_right


Now we will create two templates to demonstrate now tag. Create a template in geeks.html,

filter_none

edit
close

play_arrow

link
brightness_4
code

<html>
<h1>Template 1</h1>
<!-- Link to template 2 -->
<a href = "{% url 'template2' %}">Go to template 2</a>
</html>

chevron_right


Create a template in geeks.html,

filter_none

edit
close

play_arrow

link
brightness_4
code

<html>
<<h2>Template 2</h2>
<!-- Link to template 1 -->
<a href = "{% url 'template1' %}">Go to template 1</a>
</html>

chevron_right


Now visit http://127.0.0.1:8000/1,

url-django-template-tags
Click on the link and it willl redirect to other url.

url-djago-templtae-tags

Advanced Usage

suppose you have a view, app_views.client, whose URLconf takes a client ID (here, client() is a method inside the views file app_views.py). The URLconf line might look like this:

path('client/<int:id>/', app_views.client, name='app-views-client')

If this app’s URLconf is included into the project’s URLconf under a path such as this:

path('clients/', include('project_name.app_name.urls'))

…then, in a template, you can create a link to this view like this:

{% url 'app-views-client' client.id %}

The template tag will output the string /clients/client/123/.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.