Django REST Framework tutorial not working

django, django-rest-framework, python

Solution

This is an issue with DRF not handling namespaced urls correctly and therefore not allowing to handle your use case, please check out this thread for more details.

Problem

I want to make an API for my project and I found Django REST Framework - http://django-rest-framework.org/ I tried their tutorial here http://django-rest-framework.org/tutorial/quickstart. The only diference is that my app is called api. My problem is that when I login with my admin user I receive the following error: ``` Exception Type: NoReverseMatch Exception Value: Reverse for 'user-list' with arguments '()' and keyword arguments '{}' not found. ``` I tried to find a solution, but the result is that I am asking here if anyone has an idea :) 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')) ) ``` Here is the full stack trace: ``` 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. ```

Original source

Related problems