WeGi
WeGi

Reputation: 1946

django does not match pattern called in template

Iam trying to call {% url %} in a template to have dynamic links, but i get an error although the pattern should match. Here are my files:

urls.py

urlpatterns = [
    url(r'^(?P<team_id>[0-9]+)/$', views.detail, name='detail'),
    url(r'^$', views.index, name='index'),
]

template file

{% for team in teams_list %}
            <li><a href="{% url 'detail' team.id %}/">{{ team.name }}</a></li>
        {% endfor %}

views.py

def detail(request, team_id):
    team = get_object_or_404(Team, id=team_id)
    context = {
        'teamname' : team.name,
        'member_list' : User.objects.filter(team__name=team.name)
    }
    return render(request, 'teams/detail.html', context)

I tried it doing analog to the django tutorial, but i can't seem to find my error. the error statement is the following:

Reverse for 'detail' with arguments '(1,)' and keyword arguments '{}' not found. 0 pattern(s) tried: []

Upvotes: 0

Views: 490

Answers (1)

Burhan Khalid
Burhan Khalid

Reputation: 174622

The clue is in the error message, specifically and keyword arguments '{}' not found..

Your pattern has keyword matching, the result will be mapped to the keyword team_id, however you are passing a positional argument of 1 (that's why your team id is showing in the tuple (1,)).

To fix this, change your url tag to {% url 'detail' team_id=team.id %}.

You can read more about this at the url tag documentation.

Upvotes: 3

Related Questions