Reputation: 191
I'm new in Django and making a simple blog to improve my skills. I couldn't understand the purpose of using {% url XXX %} with slug.More precisely;
<a href ="{% url 'theview' post.slug%}">
As i know, url tag above will map the link to view function named 'theview'. And also there is a regular expression filter on url.py to catch clicked link and match it to the appropriate view function. Then why we use {%url%} although there is a filter to notice if the link is slug or not? Isn't it enough to create link like;
<a href="{{post.slug}}">
Upvotes: 0
Views: 1033
Reputation: 11695
We use url
tag to generate uri with given names and arguments and keyword arguments. If you don't want to use then you need to manually write every url. That's a bad practice.
url(r'^blog/post/(?P<slug>[\w-]+)/$', name='post_detail')
If you have url like above then (Best way to it)
# post.slug = 'learn-python'
<a href="{% url 'post_detail' post.slug %}" > {{ post }}</a>
# is equivalent to
# /blog/post/learn-python/
otherwise we need to write like
<a href="/blog/post/{{post.slug}}/" > {{ post }}</a>
<a href="{{post.slug}}">
will not work.
Upvotes: 1