U'rest_framework 'is not a registered namespace

I am trying to authenticate using the Django Rest Framework, but I cannot log in through the DRF panel. When I try to go to the login page by specifying

/ api / api authentication / Login /

NoReverseMatch at /api/api-auth/login/ u'rest_framework' is not a registered namespace Request Method: GET Request URL: http://127.0.0.1:8000/api/api-auth/login/ Django Version: 1.7.1 Exception Type: NoReverseMatch Exception Value: u'rest_framework' is not a registered namespace Exception Location: /home/shivani/aubergine_cubii/test_rest_api2/forked_rest_api/venv/local/lib/python2.7/site-packages/django/core/urlresolvers.py in reverse, line 547 Python Executable: /home/shivani/aubergine_cubii/test_rest_api2/forked_rest_api/venv/bin/python Python Version: 2.7.8 Python Path: ['/home/shivani/aubergine_cubii/test_rest_api2/forked_rest_api', '/home/shivani/aubergine_cubii/test_rest_api2/forked_rest_api/venv/lib/python2.7', '/home/shivani/aubergine_cubii/test_rest_api2/forked_rest_api/venv/lib/python2.7/plat-x86_64-linux-gnu', '/home/shivani/aubergine_cubii/test_rest_api2/forked_rest_api/venv/lib/python2.7/lib-tk', '/home/shivani/aubergine_cubii/test_rest_api2/forked_rest_api/venv/lib/python2.7/lib-old', '/home/shivani/aubergine_cubii/test_rest_api2/forked_rest_api/venv/lib/python2.7/lib-dynload', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/home/shivani/aubergine_cubii/test_rest_api2/forked_rest_api/venv/local/lib/python2.7/site-packages', '/home/shivani/aubergine_cubii/test_rest_api2/forked_rest_api/venv/lib/python2.7/site-packages'] Server time: Tue, 20 Jan 2015 10:52:13 +0000 

urls.py

 urlpatterns = patterns( '', url(r'^api/', include('api.urls', namespace='api')), url(r'^admin/', include(admin.site.urls)), url(r'^oauth2/', include('oauth2_provider.urls', namespace='oauth2_provider')) ) 

api / urls.py:

 urlpatterns += [ url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')), ] 

What should I do?

+7
source share
4 answers

The problem is with your namespaces. In particular, you are using a nested namespace, and the Django REST framework did not expect this to happen.

The API login tutorial for viewing recommends the following code snippet for API URLs

 # The API URLs are now determined automatically by the router. # Additionally, we include the login URLs for the browsable API. urlpatterns = [ url(r'^', include(router.urls)), url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')) ] 

This way, your login URLs will be located in /api-auth/ and have a rest_framework namespace, so they will not interfere with existing url patterns. This guide assumes that you are in the root urlconf when you insert templates, or at least that you are not using additional namespaces. This is because the rest_framework:login url is used to create a login page for the viewable API , so the namespace should be rest_framework .

In your case, you register the URLs under the api , so the view name is actually api:rest_framework:login . The error you get

u'rest_framework 'is not a registered namespace

Because the namespace rest_framework not the root namespace. You can fix this by moving urlpattern outside of api/urls.py or by overriding the API templates you are viewing .

+7
source

Try adding the url(r'^api-auth/', include('rest_framework.urls',namespace='rest_framework')) string url(r'^api-auth/', include('rest_framework.urls',namespace='rest_framework')) to the main urls.py or change the namespace from api/ to rest_framework instead (and remove it from another URLs) ...

+5
source

If you are not using the namespace from the API, you can simply remove the namespace from your route

 url(r'^api/', include('api.urls', namespace='api')) 

I have the same problem and it works for me because I do not use the namespace from the API anywhere

0
source

put this line in your URL section: url (r '^ api-auth /', include ('rest_framework.urls', namespace = 'rest_framework')),

0
source

Source: https://habr.com/ru/post/981271/


All Articles