Shi*_*rma 9 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
Run Code Online (Sandbox Code Playgroud)
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'))
)
Run Code Online (Sandbox Code Playgroud)
API/urls.py:
urlpatterns += [
url(r'^api-auth/', include('rest_framework.urls',
namespace='rest_framework')),
]
Run Code Online (Sandbox Code Playgroud)
我该怎么办?
问题出在你的命名空间上.具体来说,您使用的是嵌套命名空间,而Django REST框架并不期望这样.
登录可浏览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'))
]
Run Code Online (Sandbox Code Playgroud)
因此,您的登录URL将位于/api-auth/并具有命名空间rest_framework,因此不会干扰现有的URL模式.本教程假设您在放置模式时位于根urlconf中,或者至少您没有使用额外的命名空间.这是因为url rest_framework:login用于生成可浏览API的登录页面,因此名称空间必须是rest_framework.
在您的情况下,您正在注册URL api,因此视图名称实际上是api:rest_framework:login.你得到的错误
u'rest_framework'不是注册的命名空间
是因为rest_framework命名空间不是根命名空间.您可以通过将urlpattern移到外部api/urls.py或覆盖可浏览的API模板来解决此问题.
尝试添加行url(r'^api-auth/', include('rest_framework.urls',namespace='rest_framework')),你的主urls.py或改变namespace的api/,以rest_framework代替(和其他URL删除)...
| 归档时间: |
|
| 查看次数: |
4813 次 |
| 最近记录: |