django与模板中调用的模式不匹配

时间:2015-04-26 12:25:24

标签: python regex django django-templates django-urls

Iam尝试在模板中调用urlpatterns = [ url(r'^(?P<team_id>[0-9]+)/$', views.detail, name='detail'), url(r'^$', views.index, name='index'), ] 以获得动态链接,但是虽然模式应该匹配,但我收到错误。这是我的文件:

urls.py

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

模板文件

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)

views.py

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

我尝试过对django教程进行模拟,但我似乎无法找到我的错误。错误语句如下:

Shape { Long Id, String Name }

Circle :: Shape { Long Id, Integer Radius, Long Shape.Id, String Shape.Name }
Square :: Shape { Long Id, Integer Width, Long Shape.Id, String Shape.Name }

1 个答案:

答案 0 :(得分:3)

线索出现在错误消息中,特别是 和关键字参数&#39; {}&#39;找不到。

您的模式具有关键字匹配,结果将映射到关键字team_id,但是您传递的是1位置参数(这就是为什么您的团队ID显示在元组(1,))中。

要解决此问题,请将您的网址标记更改为{% url 'detail' team_id=team.id %}

您可以在url tag documentation了解更多相关信息。

相关问题