0

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?

alex
  • 2,381
  • 4
  • 23
  • 49
  • 1
    Is your url inside a namespaced app? – Sayse Jan 10 '17 at 11:55
  • main url is this: url(r'^', include('books.urls', namespace='books')), – alex Jan 10 '17 at 11:59
  • 3
    so you need to use `{% url 'books:example' yearvar %}` – devxplorer Jan 10 '17 at 12:05
  • ok yea i got it, it was from the namespace, as long as the namespace was there i couldn't use any of those. But why is the namespace affecting the template url? – alex Jan 10 '17 at 12:09
  • Possible duplicate of [What is a NoReverseMatch error, and how do I fix it?](http://stackoverflow.com/questions/38390177/what-is-a-noreversematch-error-and-how-do-i-fix-it) – Sayse Jan 10 '17 at 13:13

1 Answers1

1

There were 2 possible solutions to my problem:

  1. Removing the books namespace from my main urls.py.

  2. Adding the namespace in the django template url as devxplorer mentioned.

    <a href="{% url 'books:example' 2014 %}">2014 Archive</a>
    
Alasdair
  • 298,606
  • 55
  • 578
  • 516
alex
  • 2,381
  • 4
  • 23
  • 49