具有正确参数的Django NoReverseMatch

时间:2013-12-07 02:18:36

标签: python django

在浏览了类似答案的页面之后,我很遗憾地说我仍然难过。我很确定这是urlconf中的错误但是anywho,这里是所有相关信息:

APP中的URLCONF

urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^tenants/$', views.tenant_index, name = 'tenant_index'),
url(r'^(?P<property_alias>[\w\-]+)/$', views.property_detail, name='property_detail'),
url(r'^tenants/(<?P<first>\w+)/(<?P<last>)\w+/$', views.tenant_detail, name = 'tenant_detail'),)

的index.html

    <h1> List of Tenants</h1>
    {% for tenant in tenants %}
    <ul> {{ tenant}} </ul>
    <h2><a href="{% url 'tenant_detail' first=tenant.first_name last=tenant.last_name %}"> details </a> </h2>
    {% endfor %}

views.py段

def tenant_detail(request, first, last):
    tenant = Tenant.objects.filter(first_name__startswith = first,
        last_name__startswith = last)
    tenant = get_object_or_404(Tenant, pk=tenant[0].pk)
    return render(request, 'my_properties/tenants/tenant_detail.html', {'tenant': tenant})

错误本身是:

NoReverseMatch at /properties/tenants/

    Reverse for 'tenant_detail' with arguments '()' and keyword arguments '{u'last': u'no', u'first': u'yay'}' not found. 1 pattern(s) tried: ['properties/tenants/(<?P<first>\\[0-9A-Za-z._%+-]+)/(<?P<last>)\\[0-9A-Za-z._%+-]+/$']

    Request Method:     GET
    Request URL:    http://127.0.0.1:8000/properties/tenants/
    Django Version:     1.6
    Exception Type:     NoReverseMatch
    Exception Value:    


    Reverse for 'tenant_detail' with arguments '()' and keyword arguments '{u'last': u'no', u'first': u'yay'}' not found. 1 pattern(s) tried: ['properties/tenants/(<?P<first>\\[0-9A-Za-z._%+-]+)/(<?P<last>)\\[0-9A-Za-z._%+-]+/$']

有谁知道什么是错的?好像我正在遵循正确的一般程序

1 个答案:

答案 0 :(得分:1)

错误在你的正则表达式中。 'last'组的\w+位于括号内,并且命名组语法上存在拼写错误。

这应该有效:r'^tenants/(?P<first>\w+)/(?P<last>\w+)/$'