Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

u'rest_framework' is not a registered namespace

I am trying to do Authentication using Django Rest Framework but i am not able to login through DRF panel. When I try to get to the login page by specifying

/api/api-auth/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?

like image 727
Shivani Sharma Avatar asked Jan 20 '15 11:01

Shivani Sharma


3 Answers

The issue is with your namespaces. Specifically, you are using a nested namespace and Django REST framework was not expecting that to be the case.

The tutorial for logging into the browsable API recommends the following snippet of code for your 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'))
]

So your login urls will be located at /api-auth/ and have a namespace of rest_framework, so the don't interfere with existing url patterns. This tutorial assumes that you are in the root urlconf when you are putting in the patterns, or at least that you are not using extra namespaces. This is because the url rest_framework:login is used to generate the login page for the browsable API, so the namespace must be rest_framework.

In your case, you are registering the urls under api, so the view name is actually api:rest_framework:login. The error that you are getting

u'rest_framework' is not a registered namespace

Is because the rest_framework namespace is not a root namespace. You can fix this by moving the urlpattern outside of api/urls.py, or overriding the browsable API templates.

like image 121
Kevin Brown-Silva Avatar answered Oct 19 '22 01:10

Kevin Brown-Silva


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

like image 6
Bernhard Vallant Avatar answered Oct 19 '22 01:10

Bernhard Vallant


In my case I put this line: path('api-auth/', include('rest_framework.urls')), inside my app level urls.py when you should put it in your project level urls.py.

Hope it helps :)

like image 2
daniel eidlin Avatar answered Oct 19 '22 03:10

daniel eidlin