Django url NoReverseMatch错误

时间:2016-11-01 13:27:17

标签: django django-urls

我有下一个网址文件

urlpatterns = patterns('callboard',
    url(r'^createadv/', 'views.createadv', name='createadv'),
    url(r'^editadvert/(?P<adv_id>\d+)/$', 'views.editadvert', name='editadvert'),
    url(r'^advdetail/(?P<pk>\d+)/$', 'views.advdetail', name='advdetail'),

    url(r'^',ProductListView.as_view() , name='callboard'),
    url(r'^product/(?P<category>[0-9A-Za-z._%+-]+)', ProductListView.as_view(), name='category'),
    url(r'^product/(?P<category>[0-9A-Za-z._%+-]+)/(?P<subcategory>[0-9A-Za-z._%+-]+)', ProductListView.as_view(), name='subcategory'),
    url(r'^notes/', 'views.notes', name='notes'),
    url(r'^get_subcategory/(?P<category_id>[0-9]+)/$', 'views.get_subcategory', name='get_subcategory'),
    url(r'^get_attribute_form/(?P<subcategory_id>[0-9]+)/$', 'views.get_attribute_form', name='get_attribute_form'),
    )

当我输入 advdetail 网址时出现错误

NoReverseMatch at /callboard/advdetail/38/
Reverse for 'subcategory' with arguments '('mototransport',)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['callboard/product/(?P<category>[0-9A-Za-z._%+-]+)/(?P<subcategory>[0-9A-Za-z._%+-]+)']
Request Method: GET
Request URL:    http://127.0.0.1:8000/callboard/advdetail/38/
Django Version: 1.8.15
Exception Type: NoReverseMatch
Exception Value:    
Reverse for 'subcategory' with arguments '('mototransport',)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['callboard/product/(?P<category>[0-9A-Za-z._%+-]+)/(?P<subcategory>[0-9A-Za-z._%+-]+)']

同时 createadv 正常工作。请帮我发现,哪里出错了?

1 个答案:

答案 0 :(得分:2)

您的“子类别”网址需要两个参数 - 类别和子类别 - 但您只提供一个参数。