简体   繁体   English

具有正确参数的Django NoReverseMatch

[英]Django NoReverseMatch with correct arguments

After looking through pages of similar answers, I regret to say that I'm still stumped. 浏览了类似答案的页面后,我很遗憾地说我仍然很困惑。 I'm pretty sure it's an error in the urlconf but anywho, here's all the relative info: 我很确定这是urlconf中的错误,但无论如何,这是所有相关信息:

URLCONF in APP 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 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 %}

segment of views.py 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})

the error itself is: 错误本身是:

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._%+-]+/$']

Does anyone know what's wrong? 有人知道怎么了吗? Seems like I'm following the correct general procedure 似乎我正在遵循正确的一般程序

The error is in your regexp. 该错误在您的正则表达式中。 The \\w+ of the 'last' group was outside the bracket, and there is a typo on the named group syntax. “最后一个”组的\\w+在括号之外,并且在命名组语法上有一个错字。

This should work: r'^tenants/(?P<first>\\w+)/(?P<last>\\w+)/$' 这应该起作用: r'^tenants/(?P<first>\\w+)/(?P<last>\\w+)/$'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM