我想为我的项目制作一个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.*)$', '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.
mariodev.. 7
这是DRF无法正确处理命名空间URL的问题,因此不允许处理您的用例,请查看此主题以获取更多详细信息.
这是DRF无法正确处理命名空间URL的问题,因此不允许处理您的用例,请查看此主题以获取更多详细信息.
我在教程中遇到了同样的问题,我解决了它为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) })