Create links in template with URL path name

| Tag python  django  url  templates 

We define URL path name in urls.py:

# …
urlpatterns = [
	path('', views.news, name='news'),
]

In template file, we link it with {% url %} and path name:

<nav>
      <a class="nav-link" href="{% url 'home' %}">Home</a>
      <a class="nav-link" href="{% url 'news' %}">News</a>
</nav>

If the view requires parameters, we can pass them as well:

<a href="{% url 'detail' movie.id %}">{{ movie.title }}</a>

Prev     Next