Templates में {% url %} Tag
In this page:
{% 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.
<!-- 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 करें।
उदाहरण: 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.
<!-- 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.
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. #}
- Links को <a href="/blog/5/"> के रूप में hardcode करना {% url %} के बजाय, जो अगर URL pattern कभी बदल जाए तो silently टूट जाता है।
- {% url %} को required URL parameters pass करना भूल जाना, जो render time पर एक NoReverseMatch error का कारण बनता है।
- एक 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 किए जाते हैं।
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: