Django url 调度程序未识别视图

Django url dispatcher not identifying the view

这是我的url模式

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})

当 url 是 .../volunteer/ 时,它会打印 XKCD。但是当 url 是 ..../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.

我该怎么办?

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

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

类似

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