I have a problem with reverse in django, i've tryed different ways as in docs on different urls with the same result. For example:
url:
url(r'^(?P<year>[0-9]{4})/$', views.example, name='example'),
view:
def example(request, year):
data = dict()
books_year = Book.objects.filter(publication_date__year=year)
data['books_year'] = books_year
return render(request, 'example.html', data)
example.html:
{% if books_year %}
{% for book in books_year %}
<tr>
<td>
{{ book.title }} |
{% for authors in book.authors.all %}
{{ authors }}
{% endfor %} |
{{ book.publisher }} |
{{ book.publication_date }}
</td>
</tr>
<tr>
<td>
<a href="/2014/">2014 Archive</a>
<a href="{% url 'example' 2014 %}">2014 Archive</a>
<ul>
{% for yearvar in year_list %}
<li><a href="{% url 'example' yearvar %}">{{ yearvar }} Archive</a></li>
{% endfor %}
</ul>
</td>
</tr>
{% endfor %}
{% endif %}
If i do it with this <a href="/2014/">2014 Archive</a>
it works.
If i try it with this <a href="{% url 'example' 2014 %}">2014 Archive</a>
it gives me this error:
django.urls.exceptions.NoReverseMatch: Reverse for 'example' with arguments '(2014,)' and keyword arguments '{}' not found. 0 pattern(s) tried: []
And finally when i'm trying like this:
<ul>
{% for yearvar in year_list %}
<li><a href="{% url 'example' yearvar %}">{{ yearvar }} Archive</a></li>
{% endfor %}
</ul>
The page runs but it's not showing the link. The page should contain a link that will redirect to 2014 instead of other year.
I tryed on different views, htmls and urls and there's the same error when i'm trying to use them. Can anyone explain why?