第一次发帖所以我希望我做得对。
我正在“myapp / admin.py”中创建自定义管理表单,我正在使用该行:default_storage.exists('/usr/tmp/somefile.txt')
。
但是,exists()
会抛出SuspiciousOperation
例外。我看一下它,问题似乎来自“_os.py”中的safe_join(base, *paths)
。
事实证明基本参数设置为'/'
,这打破了safe_join()
中的以下代码:
if not normcase(final_path).startswith(normcase(base_path)) \
or final_path[base_path_len:base_path_len+1] not in ('', sep):
raise ValueError('The joined path (%s) is located outside of the base '
'path component (%s)' % (final_path, base_path))
在default_storage.exists()
中使用manage.py shell
时,我不会遇到此问题。另外,我正在使用Django 1.4和Python 2.7。
任何帮助表示赞赏!提前谢谢!
答案 0 :(得分:0)
听起来好像你的settings.py中没有正确设置
您看到SuspiciousOperation
异常的可能原因是因为您的default_storage.location设置为/
root,我认为访问文件系统的根目录肯定是可疑的。
您可能需要检查是否已将settings.py中的MEDIA_ROOT
设置为合理的存储位置:
https://docs.djangoproject.com/en/1.4/ref/settings/#media-root
似乎default_storage.location值来自MEDIA_ROOT?对我来说,它等于那个值。当我尝试在高于该位置的路径上执行default_storage.exists()时,它也会引发该异常。