1

下面是我的 django 路由配置:

# main module
urlpatterns = patterns('',
    url(r'^api/', include('api.urls')),
)

# api module
urlpatterns = patterns('',
    url(r'^$', views.index, name='index'),
    url(r'^users/(?P<id>\d+)/?$', views.users, name='users')
)

我可以访问特定用户(使用 ID),http://localhost:8000/api/users/1/但我无法访问用户列表http://localhost:8000/api/users/::

Using the URLconf defined in duck_rip.urls, Django tried these URL patterns, in this order:
^api/ ^$ [name='index']
^api/ ^users/(?P<id>\d+) [name='users']
The current URL, api/users/, didn't match any of these.

在此之前,我的模块网址是这样的:

url(r'^users/$', views.users, name='users')

我可以访问http://localhost:8000/api/users/. 有人可以解释一下我犯了什么错误吗?

4

1 回答 1

4

只需将 id 设为可选:

url(r'^users/(?:(?P<id>\d+)/)?$', views.users, name='users')

并认为:

def users(request, id=None)
于 2013-08-05T16:47:34.050 回答