Blog

What is Django URL Tag in Django Template Language

Django URL Tag in Django Template Language copy
Django Django Tutorials

What is Django URL Tag in Django Template Language

What is Django URL Tag in Django Template Language

What is Django URL Template Tag?

URL is the uniform resource locator. On the website, there is a number of web pages having unique URL. You have to create a URL for your web page, the user hits that URL to reach a specific web page. We are learning the Django URL tag in the Django template language. We learn different terminologies in DTL (Django template language) as variable, tags, filter, conditions, loops, etc. In this Django tutorial, we will learn the URL tag that is used in the Django template. Because we did not use python programming in the template, for this reason we have to use another technique that works properly between HTML and Python. So URL tag is also one of them which is used to connect different web pages to each other as we used HTML hyperlinks.

Django URL Tag Syntax

It is the basic syntax of the Django URL tag which you used in the template in order to connect to another web page.

There are different parts of this syntax, each part, we discuss to learn deeply. It is the basic syntax of the Django URL tag which we will learn its different parts/arguments.

{% url ‘value of Name attribute ‘ var1 var2 … ‘%}

  1. {% %}:                                   It is showing that it is the DTL tag
  2. ‘url’:                                       Inside {% %} url is showing this tag is related to creating a URL for another web page.
  3. ‘value of Name attribute’:    It means, you have to mention the value of the name attribute for that web page which you want to link. This value of name attribute you will find in the urls.py file in which you created a path for that web page.

In the path, you have to mention the value in the name attribute, when you give name value in the URL tag when a user hits the URL tag link, then the user will redirect to that path from you take name value.

path(‘urlname/’, views.viewname, name=‘valuehere’)

4. ‘ var1 var2 … ‘:                    It means if you want to pass a variable to URL as getting variable, you can pass here. For example, sometimes we need to pass the id to the URL.

Example to more understand Django URL tag

If you have two web pages in your testing website:

  • index.html
  • about.html

if you want to link both web pages, what you have to do. First of all, go to the index.html file and make a link for about.html like this.

<a href=”{% url ‘name_of_about_url’  %}” >About Page<a/> <!– It is the URL tag –>

When anyone clicks on the About Page, it will redirect to the about.html page because we linked that. Actually, name_of_about_url is the value of the name attribute which we gave in the urls.py file when creating a path for about.html.

How to use Django URL tag in Django Application

  • Create Django Project
  • Make an application & include the application inside the settings.py file of the project
  • Creating a template for frontend view
  • Writing a view function and rendering a template that you created
  • Making a path (URL pattern) for that view section in urls.py file
  • Creating the 2nd template for 2nd view
  • Now, create 2nd view function
  • Rendering 2nd template in 2nd view
  • Write a path for 2nd views in the urls.py file
  • Now open 1st template and make use of the Django URL tag
  • When anyone clicks that URL, the user will redirect to 2nd template

If you follow the above steps you will be able to create a link between two web pages. If you have an existing project then don’t need to apply steps just need two web page to link them.

Conclusion

We have learned that how we can use the Django URL tag in the Django template according to DTL. Surely, now you can connect the web pages using URL tags instead of hyperlinks. Because using a URL tag is more recommended than HTML hard-coded hyperlink. In short, the link created with the Django URL tag is more beneficial than hardcoded because in the future, if you want to change the permalinks of your web page or the name of the template then it will not affect your connected page.

If you have any questions in your mind while reading out the tutorial, then must share them with us. We will provide you the best solution possible.

If you use Facebook, Twitter, Pinterest, LinkedIn, or other social media platforms, then share this Django tutorial, if you share, more students will take the chance to learn.

Recommended Django Tutorial for Beginners

Django Tutorial for Beginners | Django Tutorials series 

How to Make Django Dynamic URLs Pattern | Django Tutorials

Hack – Django Website Tutorials | Building Complete Website with Django

ALL About Django Form Errors | Django Tutorials

Top 20 Python Real World Applications | Python tutorials

Leave your thought here

Your email address will not be published.

Select the fields to be shown. Others will be hidden. Drag and drop to rearrange the order.
  • Image
  • SKU
  • Rating
  • Price
  • Stock
  • Availability
  • Add to cart
  • Description
  • Content
  • Weight
  • Dimensions
  • Additional information
  • Attributes
  • Custom attributes
  • Custom fields
Click outside to hide the compare bar
Compare
Wishlist 0
Open wishlist page Continue shopping