Django REST框架教程无法运行

5

我想为我的项目创建一个API,并找到了Django REST Framework - http://django-rest-framework.org/

我在这里尝试了他们的教程http://django-rest-framework.org/tutorial/quickstart。唯一的区别是我的应用程序名叫api。 我的问题是,当我使用管理员用户登录时,我会收到以下错误:

Exception Type: NoReverseMatch
Exception Value:    
Reverse for 'user-list' with arguments '()' and keyword arguments '{}' not found.

我尝试找到解决方案,但结果是我在这里询问是否有人有想法 :) urls.py
from django.conf.urls import patterns, include, url
from django.conf.urls.static import static
from django.conf import settings

#not sure
from django.contrib.staticfiles.urls import staticfiles_urlpatterns

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

from dajaxice.core import dajaxice_autodiscover, dajaxice_config
dajaxice_autodiscover()

js_info_dict = {
    'packages': ('cards',),
}

urlpatterns = patterns('',
    # Examples:
    url(r'^$', include('cards.urls', namespace='cards')),
    # url(r'^giftycards/', include('giftycards.foo.urls')),
    url(r'^cards/', include('cards.urls', namespace='cards')),
    url(r'^api/', include('api.urls', namespace='api')),


    # Uncomment the admin/doc line below to enable admin documentation:
    url(r'^admin/doc/', include('django.contrib.admindocs.urls')),

    # Uncomment the next line to enable the admin:
    url(r'^admin/', include(admin.site.urls)),
    url(dajaxice_config.dajaxice_url, include('dajaxice.urls')),

    # REST API
    url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')),

    # Internationalization urls
    url(r'^i18n/', include('django.conf.urls.i18n')),
    url(r'^jsi18n/$', 'django.views.i18n.javascript_catalog', js_info_dict),
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

urlpatterns += staticfiles_urlpatterns()

urlpatterns += patterns('',
    url(r'^static/(?P<path>.*)$', 'django.views.static.serve',
    {'document_root': settings.STATIC_ROOT }),

)

api/urls.py

from django.conf.urls import patterns, url, include
from rest_framework import routers
from api import views

router = routers.DefaultRouter()
router.register(r'users', views.UserViewSet)
router.register(r'groups', views.GroupViewSet)

# Wire up our API using automatic URL routing.
# Additionally, we include login URLs for the browseable API.
urlpatterns = patterns('',
    url(r'^', include(router.urls)),
    url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework'))
)

以下是完整的堆栈跟踪:

Environment:


Request Method: GET
Request URL: http://localhost:1238/api/

Django Version: 1.5.4
Python Version: 2.7.3
Installed Applications:
('django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.admin',
 'django.contrib.admindocs',
 'dajaxice',
 'rest_framework',
 'api',
 'cards')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.locale.LocaleMiddleware')


Traceback:
File "/home/valentin/Documents/Dev/giftycards/libs/django/core/handlers/base.py" in get_response
  115.                         response = callback(request, *callback_args, **callback_kwargs)
File "/home/valentin/Documents/Dev/giftycards/libs/django/views/generic/base.py" in view
  68.             return self.dispatch(request, *args, **kwargs)
File "/home/valentin/Documents/Dev/giftycards/libs/django/views/decorators/csrf.py" in wrapped_view
  77.         return view_func(*args, **kwargs)
File "/home/valentin/Documents/Dev/giftycards/libs/rest_framework/views.py" in dispatch
  399.             response = self.handle_exception(exc)
File "/home/valentin/Documents/Dev/giftycards/libs/rest_framework/views.py" in dispatch
  396.             response = handler(request, *args, **kwargs)
File "/home/valentin/Documents/Dev/giftycards/libs/rest_framework/routers.py" in get
  254.                     ret[key] = reverse(url_name, request=request, format=format)
File "/home/valentin/Documents/Dev/giftycards/libs/rest_framework/reverse.py" in reverse
  17.     url = django_reverse(viewname, args=args, kwargs=kwargs, **extra)
File "/home/valentin/Documents/Dev/giftycards/libs/django/core/urlresolvers.py" in reverse
  496.     return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
File "/home/valentin/Documents/Dev/giftycards/libs/django/core/urlresolvers.py" in _reverse_with_prefix
  416.                 "arguments '%s' not found." % (lookup_view_s, args, kwargs))

Exception Type: NoReverseMatch at /api/
Exception Value: Reverse for 'user-list' with arguments '()' and keyword arguments '{}' not found.

你能发一下你的urls.py文件吗?看起来你漏掉了'user-list'视图的路由。 - Fiver
我已将其添加到主帖中。我提到,与教程不同的是我的应用程序名称是api而不是quickstart。 - valkirilov
我认为你需要将api-auth的url模式从应用级别的urls.py文件移动到项目级别的urls.py文件中。 - highpost
2个回答

7

这是一个 DRF 处理命名空间 URL 不正确的问题,因此无法处理您的用例,请查看此线程以获取更多详细信息。


5

我和这篇教程遇到了相同的问题,我解决了它通过为url指定一个URL模式名称(请注意name参数):

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

然后使用此路径代替Python路径:

def api_root(request, format=None):
return Response({
    'users': reverse('users', request=request, format=format)
})

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接