Django url调度程序无法识别视图

时间:2016-01-04 19:03:52

标签: django url-routing django-urls url-pattern

这是我的urlpatterns

urlpatterns = [
    url(r'^volunteer/$', views.volunteerInformation, name='volunteerInformation'),
    url(r'^volunteer/(?P<ID>[0-0]{1})/$', views.volunteerInformation, name='volunteerInformation'),
]

以下是我试图致电

的观点
def volunteerInformation(request, ID=None):
    volunteers = Volunteer.objects.all()
    if ID:
        print ID
    else:
        print "XKCD"
    return render(request, 'dbaccess/volunteer.html', {'volunteers': volunteers})

当网址是... /志愿者/时,它会打印XKCD。但是当网址是.... / volunteer / 1时,我收到的错误是找不到该页面。这是错误:

^ ^volunteer/(?P<ID>[0-0]{1})/$ [name='indVolunteerInformation']
^ ^volunteer/$ [name='volunteerInformation']
^admin/
The current URL, volunteer/3, didn't match any of these.

我该怎么办?

1 个答案:

答案 0 :(得分:2)

您的网址正则表达式错误,您正在搜索0-0范围内的长度为1的数字。要匹配任何数字,请更改:

^volunteer/(?P<ID>[0-0]{1})/$

类似

^volunteer/(?P<ID>\d+)/$