u'rest_framework'不是注册的命名空间

时间:2015-01-20 11:26:59

标签: python django django-rest-framework

我正在尝试使用Django Rest Framework进行身份验证,但我无法通过DRF面板登录。当我尝试通过指定

进入登录页面时

/ API / API-AUTH /登录/

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')),
]

我该怎么办?

5 个答案:

答案 0 :(得分:7)

问题在于您的命名空间。具体来说,您使用的是嵌套命名空间,而Django REST框架并不期望这样。

logging into the browsable API教程为您的API网址推荐以下代码段

# 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'))
]

因此,您的登录网址位于/api-auth/且名称空间为rest_framework,因此不会干扰现有的网址格式。本教程假设您在放置模式时位于根urlconf中,或者至少您没有使用额外的命名空间。这是因为网址rest_framework:login用于生成可浏览API的登录页面,因此命名空间必须为rest_framework

在您的情况下,您正在api下注册网址,因此视图名称实际上是api:rest_framework:login。你得到的错误

  

U' rest_framework'不是注册名称空间

是因为rest_framework命名空间不是根命名空间。您可以通过将网址模式移到api/urls.pyoverriding the browsable API templates之外来解决此问题。

答案 1 :(得分:5)

尝试将行url(r'^api-auth/', include('rest_framework.urls',namespace='rest_framework'))添加到您的主urls.py或将namespace的{​​{1}}更改为api/(并将其从其他网址中删除) )...

答案 2 :(得分:2)

在我的情况下,我将这一行: path('api-auth/', include('rest_framework.urls')), 应该放在我的应用程序级别urls.py中,应该将其放在项目级别urls.py中。

希望它会有所帮助:)

答案 3 :(得分:0)

如果您没有使用api中的命名空间,只需从路径中删除命名空间

即可
LogMessageWindow.AppendText(message + ".\n"

我有同样的问题并且对我有用,因为我没有使用api无处不在的命名空间

答案 4 :(得分:-1)

将此行放入您的URL部分: url(r'^ api-auth /',include('rest_framework.urls',namespace ='rest_framework')),