【问题标题】:Django url dispatcher not identifying the viewDjango url调度程序未识别视图
【发布时间】:2016-04-08 11:19:36
【问题描述】:

这是我的 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})

当 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.

我该怎么办?

【问题讨论】:

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


    【解决方案1】:

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

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

    类似的东西

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

    【讨论】:

      猜你喜欢
      • 2012-12-18
      • 2012-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-22
      • 1970-01-01
      相关资源
      最近更新 更多