我在我的项目中使用 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),
...
此问题源于使用:
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/
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
大约一年前,我决定确保每个包含非唯一文本的Flash通知都将从模块中的方法中获取文本。我这样做的最初原因是为了避免一遍又一遍地输入相同的字符串。如果我想更改措辞,我可以在一个地方轻松完成,而且一遍又一遍地重复同一件事而出现拼写错误的可能性也会降低。我最终得到的是这样的:moduleMessagesdefformat_error_messages(errors)errors.map{|attribute,message|"Error:#{attribute.to_s.titleize}#{message}."}enddeferror_message_could_not_find(obje
我遵循MichaelHartl的“RubyonRails教程:学习Web开发”,并创建了检查用户名和电子邮件长度有效性的测试(名称最多50个字符,电子邮件最多255个字符)。test/helpers/application_helper_test.rb的内容是:require'test_helper'classApplicationHelperTest在运行bundleexecraketest时,所有测试都通过了,但我看到以下消息在最后被标记为错误:ERROR["test_full_title_helper",ApplicationHelperTest,1.820016791]test
似乎无法为此找到有效的答案。我正在阅读Rails教程的第10章第10.1.2节,但似乎无法使邮件程序预览正常工作。我发现处理错误的所有答案都与教程的不同部分相关,我假设我犯的错误正盯着我的脸。我已经完成并将教程中的代码复制/粘贴到相关文件中,但到目前为止,我还看不出我输入的内容与教程中的内容有什么区别。到目前为止,建议是在函数定义中添加或删除参数user,但这并没有解决问题。触发错误的url是http://localhost:3000/rails/mailers/user_mailer/account_activation.http://localhost:3000/rails/mai
我是rails的新手,想在form字段上应用验证。myviewsnew.html.erb.....模拟.rbclassSimulation{:in=>1..25,:message=>'Therowmustbebetween1and25'}end模拟Controller.rbclassSimulationsController我想检查模型类中row字段的整数范围,如果不在范围内则返回错误信息。我可以检查上面代码的范围,但无法返回错误消息提前致谢 最佳答案 关键是您使用的是模型表单,一种显示ActiveRecord模型实例属性的表单。c
我正在尝试编写一个将文件上传到AWS并公开该文件的Ruby脚本。我做了以下事情:s3=Aws::S3::Resource.new(credentials:Aws::Credentials.new(KEY,SECRET),region:'us-west-2')obj=s3.bucket('stg-db').object('key')obj.upload_file(filename)这似乎工作正常,除了该文件不是公开可用的,而且我无法获得它的公共(public)URL。但是当我登录到S3时,我可以正常查看我的文件。为了使其公开可用,我将最后一行更改为obj.upload_file(file
我克隆了一个rails仓库,我现在正尝试捆绑安装背景:OSXElCapitanruby2.2.3p173(2015-08-18修订版51636)[x86_64-darwin15]rails-v在您的Gemfile中列出的或native可用的任何gem源中找不到gem'pg(>=0)ruby'。运行bundleinstall以安装缺少的gem。bundleinstallFetchinggemmetadatafromhttps://rubygems.org/............Fetchingversionmetadatafromhttps://rubygems.org/...Fe
在Cooper的书BeginningRuby中,第166页有一个我无法重现的示例。classSongincludeComparableattr_accessor:lengthdef(other)@lengthother.lengthenddefinitialize(song_name,length)@song_name=song_name@length=lengthendenda=Song.new('Rockaroundtheclock',143)b=Song.new('BohemianRhapsody',544)c=Song.new('MinuteWaltz',60)a.betwee
我是Google云的新手,我正在尝试对其进行首次部署。我的第一个部署是RubyonRails项目。我基本上是在关注thisguideinthegoogleclouddocumentation.唯一的区别是我使用的是我自己的项目,而不是他们提供的“helloworld”项目。这是我的app.yaml文件runtime:customvm:trueentrypoint:bundleexecrackup-p8080-Eproductionconfig.ruresources:cpu:0.5memory_gb:1.3disk_size_gb:10当我转到我的项目目录并运行gcloudprevie
我有两个Rails模型,即Invoice和Invoice_details。一个Invoice_details属于Invoice,一个Invoice有多个Invoice_details。我无法使用accepts_nested_attributes_forinInvoice通过Invoice模型保存Invoice_details。我收到以下错误:(0.2ms)BEGIN(0.2ms)ROLLBACKCompleted422UnprocessableEntityin25ms(ActiveRecord:4.0ms)ActiveRecord::RecordInvalid(Validationfa