← Back to Django Course | Chapter 3: Templates & Template Language | Lesson 10 of 10

Templates में {% url %} Tag

{% url %} tag एक page का address इसके नाम से लुकअप करता है, इसलिए links काम करते रहते हैं भले ही असली URL path बाद में बदल जाए।
Syntax
markup
{% url 'url_name' %}
{% url 'url_name' argument %}

<a href="{% url 'url_name' argument %}">Link text</a>

Hardcoding के बजाय {% url %} क्यों उपयोग करें

{% url %} इसके registered नाम से एक URL pattern लुकअप करता है और इससे असली path बनाता है, इसलिए links underlying pattern बदलने पर भी अपने आप correct रहते हैं।

उदाहरण: Why Use {% url %} Instead of Hardcoding

This renders as <a href="/about/">, generated from the URL pattern named about rather than a hardcoded string.

markup
<!-- urls.py has: path('about/', views.about, name='about') -->
<a href="{% url 'about' %}">About Us</a>
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

{% url %} को Arguments Pass करना

जब एक URL pattern में dynamic segments हों (जैसे एक ID), matching values को {% url %} में URL name के बिल्कुल बाद directly pass करें।

Warning: गलत number of arguments pass करना render time पर एक NoReverseMatch error raise करता है।

उदाहरण: Passing Arguments to {% url %}

article.id is passed as the URL argument, producing a link like /articles/7/ for an article whose id is 7.

markup
<!-- urls.py has: path('articles/<int:article_id>/', views.detail, name='article-detail') -->
<a href="{% url 'article-detail' article.id %}">{{ article.title }}</a>
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

Reuse के लिए URL Patterns को Name देना

{% url %} के साथ उपयोग के लिए intended हर path() को एक unique name declare करना चाहिए, जो वही है जो tag असल में लुकअप करता है।

उदाहरण: Naming URL Patterns for Reuse

These two names, home and about, are exactly what {% url home %} and {% url about %} in templates will resolve.

markup
from django.urls import path
from . import views

urlpatterns = [
    path('', views.home, name='home'),
    path('about/', views.about, name='about'),
]
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. Links को <a href="/blog/5/"> के रूप में hardcode करना {% url %} के बजाय, जो अगर URL pattern कभी बदल जाए तो silently टूट जाता है।
  2. {% url %} को required URL parameters pass करना भूल जाना, जो render time पर एक NoReverseMatch error का कारण बनता है।
  3. एक URL name उपयोग करना जो urls.py में असल में registered नहीं है (typo या path() पर missing name='...' argument)।
चैप्टर सारांश
  • {% url name %} एक named URL pattern के लिए सही URL generate करता है paths hardcode करने के बजाय।
  • urls.py में हर path() को {% url %} के इसे reference करने के लिए एक name='...' argument चाहिए।
  • Dynamic URL segments के लिए Arguments URL name के बिल्कुल बाद pass किए जाते हैं।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.