Imagine that you need to write a URL to refer to your FooView
, then you can write it like
<a href="/foo/">link</a>
But a problem can occur if you later change your mind, and want to use qux/
for this view. So rewriting a URL, could result in a large amount of rewrites. It is also cumbersome if the URL contains parameters. Say you have a URL with a pk
and type
parameter (like foo/<pk>/qux/<type>
), then you need to remember the format, and thus reason about the format, which is cumbersome: "is it /foo/1/qux/2
or /foo/2/qux/1
?".
So we do not want to write these URL directly, we want to determine these: calculating the URL based on some identifier, and zero or more parameters. By defining a name in a path, we thus first need to define such identifier, and we can define a name for such path.
So now we can in the template use the {% url .. %}
template tag instead, like:
<a href="{% url 'bar' %}">link</a>
Django will then do the math, and replace it with the correct URL. Not only does this save us a headache, the URL is also guaranteed to be correct (given of course the name itself is correct). So if it renders HTML output, we do not need to worry, that we might have written a typo in one of the URLs, since the {% url .. %}
tag will raise an error if it can not find the name, or in case the parameters do not match.
We can do the same when we for example want to redirect to a page. If we have a certain view that redirects to bar
, it looks like:
def redirect_view(request):
return redirect('bar')
and again, we are done. If later the URL changes, or when for example somebody wants to implemented translated URLs (for example you can run a server in English, and one in Russian), then these URLs will have the correct translation.
In case the URL contains a parameter, like for example:
urlpatterns = [
path('foo/<int:pk>/edit',views.FooView,name='bar'),
]
We can use this parameter as well. In the template it looks like:
<a href="{% url 'bar' pk=2 %}">link</a>
or in the view:
def redirect_view(request):
return redirect('bar', pk=2)
How that parameter fits in the URL, is not our problem. Django will find this out itself.
{% url 'bar' %}
in the template, orreturn redirect('bar')
in the view...