在Django中,我有一个基于类的视图:
class ApiRoot(APIView):
def get(request, format=None):
return Response({
'users': reverse('user-list', request=request, format=format),
#'snippets': reverse('snippet-list', request=request, format=format)
})
以及urls.py中的以下网址格式
urlpatterns = format_suffix_patterns([
url(r'^$', views.ApiRoot.as_view(), name='api-root'),
url(r'^snippets/$', views.SnippetList.as_view(), name='snippet-list'),
url(r'^snippets/(?P<pk>[0-9]+)/$', views.SnippetDetail.as_view(), name='snippet-detail'),
url(r'^users/$', views.UserList.as_view(), name='user-list'),
url(r'^users/(?P<pk>[0-9]+)/$', views.UserDetail.as_view(), name='user-detail'),
])
urlpatterns += [
url(r'^api-auth/', include('rest_framework.urls',
namespace='rest_framework')),
我认为我正确地连接了一些东西,但是我收到以下错误:
NoReverseMatch at /
Reverse for 'user-list' with arguments '()' and keyword arguments '{u'format': <rest_framework.request.Request object at 0x7f99998c4c50>}' not found. 2 pattern(s) tried: [u'users\\.(?P<format>[a-z0-9]+)/?$', 'users/$']
任何想法为什么?
感谢
答案 0 :(得分:4)
您应该将网址反转为reverse('user-list')
。这应该可以解决你的问题。