gpt4 book ai didi

python - 导入错误 : No module named rest_framework. 授权 token

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

我在我的项目中使用 django rest-framework (DRF) token 身份验证来在创建用户时创建 token 。在我从 DRF 文档中添加这一行之前,一切都很好:

url(r'^api-token-auth/', 'rest_framework.authtoken.views.obtain_auth_token'),

创建一个端点,在正确发布用户名和密码后为用户返回 token 。

它抛出这个错误:

ImportError: No module named rest_framework.authtoken

这很奇怪,因为 DRF 没有这一行就可以了,所以它必须包含在我的 PYTHONPATH 中。

我还运行了 python manage.py syncdb 和适当的迁移。

知道哪里出了问题吗?

设置.py:

THIRD_PARTY_APPS = (
'south', # Database migration helpers:
'crispy_forms', # Form layouts
'avatar', # for user avatars
'rest_framework', # for rest
'django_rq', # for aysnc
'rest_framework_swagger', # for exploring the api
'rest_framework.authtoken',
)
INSTALLED_APPS = DJANGO_APPS + THIRD_PARTY_APPS + LOCAL_APPS

完整的堆栈跟踪:

Unhandled exception in thread started by <function wrapper at 0x1046096e0>
Traceback (most recent call last):
File "/Users/admin/dev/ncla-web/env/lib/python2.7/site-packages/django/utils/autoreload.py", line 93, in wrapper
fn(*args, **kwargs)
File "/Users/admin/dev/ncla-web/env/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 101, in inner_run
self.validate(display_num_errors=True)
File "/Users/admin/dev/ncla-web/env/lib/python2.7/site-packages/django/core/management/base.py", line 310, in validate
num_errors = get_validation_errors(s, app)
File "/Users/admin/dev/ncla-web/env/lib/python2.7/site-packages/django/core/management/validation.py", line 34, in get_validation_errors
for (app_name, error) in get_app_errors().items():
File "/Users/admin/dev/ncla-web/env/lib/python2.7/site-packages/django/db/models/loading.py", line 196, in get_app_errors
self._populate()
File "/Users/admin/dev/ncla-web/env/lib/python2.7/site-packages/django/db/models/loading.py", line 78, in _populate
self.load_app(app_name)
File "/Users/admin/dev/ncla-web/env/lib/python2.7/site-packages/django/db/models/loading.py", line 99, in load_app
models = import_module('%s.models' % app_name)
File "/Users/admin/dev/ncla-web/env/lib/python2.7/site-packages/django/utils/importlib.py", line 40, in import_module
__import__(name)
File "/Users/admin/dev/ncla-web/env/lib/python2.7/site-packages/debug_toolbar/models.py", line 63, in <module>
patch_root_urlconf()
File "/Users/admin/dev/ncla-web/env/lib/python2.7/site-packages/debug_toolbar/models.py", line 51, in patch_root_urlconf
reverse('djdt:render_panel')
File "/Users/admin/dev/ncla-web/env/lib/python2.7/site-packages/django/core/urlresolvers.py", line 480, in reverse
app_list = resolver.app_dict[ns]
File "/Users/admin/dev/ncla-web/env/lib/python2.7/site-packages/django/core/urlresolvers.py", line 310, in app_dict
self._populate()
File "/Users/admin/dev/ncla-web/env/lib/python2.7/site-packages/django/core/urlresolvers.py", line 273, in _populate
for name in pattern.reverse_dict:
File "/Users/admin/dev/ncla-web/env/lib/python2.7/site-packages/django/core/urlresolvers.py", line 296, in reverse_dict
self._populate()
File "/Users/admin/dev/ncla-web/env/lib/python2.7/site-packages/django/core/urlresolvers.py", line 285, in _populate
lookups.appendlist(pattern.callback, (bits, p_pattern, pattern.default_args))
File "/Users/admin/dev/ncla-web/env/lib/python2.7/site-packages/django/core/urlresolvers.py", line 229, in callback
self._callback = get_callable(self._callback_str)
File "/Users/admin/dev/ncla-web/env/lib/python2.7/site-packages/django/utils/functional.py", line 32, in wrapper
result = func(*args)
File "/Users/admin/dev/ncla-web/env/lib/python2.7/site-packages/django/core/urlresolvers.py", line 100, in get_callable
not module_has_submodule(import_module(parentmod), submod)):
File "/Users/admin/dev/ncla-web/env/lib/python2.7/site-packages/django/utils/importlib.py", line 40, in import_module
__import__(name)
ImportError: No module named rest_framework.authtoken

最佳答案

查看最后的回复 here .不要将整个 View 作为字符串包含,而是先导入“obtain_auth_token”,然后再引用它。

from rest_framework.authtoken.views import obtain_auth_token

...
url(r'^api-token-auth/', obtain_auth_token),
...

agconti 的进一步更新:

此问题源于使用:

urlpatterns = patterns("api.views",
...
url(r'^api-token-auth/', 'rest_framework.authtoken.views.obtain_auth_token'),
...
)

因为 View 前缀。如果你想这样使用api-token-auth/你必须改成下面的方式或者使用Alex提供的解决方案:

urlpatterns = patterns('',
...
url(r'^api-token-auth/', 'rest_framework.authtoken.views.obtain_auth_token'),
...
)

关于python - 导入错误 : No module named rest_framework. 授权 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23251141/

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