gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 20:51:47 25 4
gpt4 key购买 nike

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

进入登录页面时

/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

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

我该怎么办?

最佳答案

问题出在您的命名空间上。具体来说,您使用的是嵌套命名空间,而 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'))
]

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

在您的例子中,您正在 api 下注册 url,因此 View 名称实际上是 api:rest_framework:login。您遇到的错误

u'rest_framework' is not a registered namespace

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

关于python - u'rest_framework' 不是注册的命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28044219/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com