Python Django行动可疑

Python Django行动可疑,python,django,Python,Django,我在尝试删除上传的图像时遇到问题 错误大致如下: SuspiciousOperation: Attempted access to '/media/artists/12-stones/154339.jpg' denied. 仔细阅读后,错误似乎是因为它在错误的位置查找映像(请注意,第一个斜杠,/media/在文件系统上不存在) 我的媒体根目录和媒体URL是: MEDIA_ROOT = '/home/tsoporan/site/media/' MEDIA_URL = "/media/ My m

我在尝试删除上传的图像时遇到问题

错误大致如下:

SuspiciousOperation: Attempted access to '/media/artists/12-stones/154339.jpg' denied.
仔细阅读后,错误似乎是因为它在错误的位置查找映像(请注意,第一个斜杠,/media/在文件系统上不存在)

我的媒体根目录和媒体URL是:

MEDIA_ROOT = '/home/tsoporan/site/media/'
MEDIA_URL = "/media/
My models upload_to parameter通过此函数传递:

def get_artist_path(instance, filename):
  return os.path.join('artists', slugify(instance.name), filename)
我的问题是:

1) 如何解决此问题以备将来上载

2) 是否可以修复当前图像的路径而不必重新加载

问候,,
Titus

好吧,代码中的一个小灰点显示,可能有一个更深层次的错误消息,在这一过程中被同质化了

在django/core/files/storage.py的第210行(在1.1.1中),我们有:

因此,错误必须来自safe_join()

在django/utils/_os.py中,我们有以下内容。注意它在第三行到最后一行抛出的ValueError:

===========================

def safe_join(base, *paths):
    """
    Joins one or more path components to the base path component intelligently.
    Returns a normalized, absolute version of the final path.

    The final path must be located inside of the base path component (otherwise
    a ValueError is raised).
    """
    # We need to use normcase to ensure we don't false-negative on case
    # insensitive operating systems (like Windows).
    base = force_unicode(base)
    paths = [force_unicode(p) for p in paths]
    final_path = normcase(abspathu(join(base, *paths)))
    base_path = normcase(abspathu(base))
    base_path_len = len(base_path)
    # Ensure final_path starts with base_path and that the next character after
    # the final path is os.sep (or nothing, in which case final_path must be
    # equal to base_path).
    if not final_path.startswith(base_path) \
       or final_path[base_path_len:base_path_len+1] not in ('', sep):
        raise ValueError('the joined path is located outside of the base path'
                         ' component')
    return final_path
==================

def safe_join(base, *paths):
    """
    Joins one or more path components to the base path component intelligently.
    Returns a normalized, absolute version of the final path.

    The final path must be located inside of the base path component (otherwise
    a ValueError is raised).
    """
    # We need to use normcase to ensure we don't false-negative on case
    # insensitive operating systems (like Windows).
    base = force_unicode(base)
    paths = [force_unicode(p) for p in paths]
    final_path = normcase(abspathu(join(base, *paths)))
    base_path = normcase(abspathu(base))
    base_path_len = len(base_path)
    # Ensure final_path starts with base_path and that the next character after
    # the final path is os.sep (or nothing, in which case final_path must be
    # equal to base_path).
    if not final_path.startswith(base_path) \
       or final_path[base_path_len:base_path_len+1] not in ('', sep):
        raise ValueError('the joined path is located outside of the base path'
                         ' component')
    return final_path
Hmmm,“连接的路径位于基本路径组件的外部”。现在这里有几个对abspathu()的调用(它是在这个例程上面定义的,NT与其他操作系统不同)。abspathu()通过在当前工作目录os.cwdu()上定位,将所有非绝对路径转换为绝对路径

问题:您是否有指向媒体目录的符号链接?换句话说,它不是项目目录的直接子目录?我不知道这是否是一个有效的问题,它只是突然从我的脑海中冒出来

问题:传递给safe_join()的
self.location
name
的值是什么

野驴猜测:
self.location
是否为空

另一个疯狂的猜测:MEDIA\u ROOT是否以某种方式被更改为
/MEDIA/

如果您安装了自己的Django副本(这并不困难),请尝试在这些例程中放入一些print语句,然后将其作为开发服务器运行。打印输出将转到控制台

更新:Hmmm。您说过“2)self.location和name的值是:/home/tsoporan/site/media和/media/albums/anthem for The underdog/30103635.jpg”

以下路径有意义吗

"/home/tsoporan/site/media/media/albums/anthem-for-the-underdog"
请注意…/media/media/。。。在那里


还有,这是什么操作系统?Django rev?

啊算出来了,有点尴尬,但结果是错误更高了。我用脚本插入这些图像,再次查看时发现我的路径以/media/开头

现在,我有大约4000个错误路径的图像。。。有没有办法修改所有这些图像的路径?还是需要重新考虑


谢谢大家,为我的错误道歉

你真的应该就此提出一个新问题。请尝试以下操作:

编写一个独立的django脚本,如下所示:

from django.core.management import setup_environ
from mysite import settings
setup_environ(settings)
from django.db import transaction

from app.models import Album # or whatever your model name is

for a in Album.objects.all():
    # Do something to cleanup the filename.
    # NOTE! This will not move the files, just change the value in the field.
    a.filename = re.sub(r'^/media', '', a.filename)
    a.save()

transaction.commit_unless_managed() # flush all changes

django 1.2.5中修复了此错误。请参见

当我在上传到定义中添加了一个前导斜杠时,出现了这个错误

坏的


如果要使用其他位置,如/data/images/myfile/,则应在django settings.py文件中将MEDIA_ROOT设置为/data/images

请注意,当您正在查找的静态文件资源中有双“/”时,可能会导致此问题

{{ STATIC_URL }}/style.css # Causes the issue it should be
{{ STATIC_URL }}style.css

我也犯了这个错误。通过调试发现引发了以下异常

SuspiciousOperation(u"Attempted access to '2015-03-19-08:29:51-2-f8945842891244629dfd0c0af4c72a9c.pdf' denied.",)
顺便说一句,我使用django存储(v1.1.8)将我的媒体文件存储到S3(使用S3boto后端)。我正在使用django 1.7.6


但如果我切换到使用文件名而不使用冒号(:)存储,它似乎可以工作。我还没有弄清楚根本原因是什么。如果这对其他人有帮助,就把它贴出来。显然,django或django storages不喜欢带有冒号的文件名。

通过使用dumb
print
语句,我发现一些媒体文件的url路径中有前缀
/media
。虽然默认存储选项可以处理此问题,但如果使用
django存储中的
S3BotoStorage
,则会出现问题

因此,我通过覆盖
\u normalize\u name
(由@peter rowell给出的答案指导)修复了它:


如果临时文件不是媒体根文件夹的一部分,请使用
SimpleUploadedFile
。这不会引发可疑操作
错误:

upload_file = SimpleUploadedFile(name=basename(out_file), content=open(out_file, 'rb').read())
object = YourModel.objects.create(file=upload_file)
如果您的临时文件已经是MEDIA_ROOT的一部分,请使用
File
。如果要将现有的Django文件链接到对象,这非常有用

object = YourModel.objects.create(file=File(open(file_path, 'rb')))

我用一种非常简单的方法解决了这个问题,转到这个文件夹中的
utils.py

djk\lib\site-packages\django\core\files\utils.py         
(djk是虚拟电视的名称)


在文件中,只需对第7行和第8行进行python注释,就这样,工作完成了。

您如何提供静态文件?您用于静态文件的url是什么。通过Apache的ProxyPass使用nginx提供静态文件:例如:ProxyPass/media ProxyPassReverse/media Edit:上述注释已删除请参见1)否我没有指向媒体目录的符号链接。2) self.location和name的值是:/home/tsoporan/site/media和/media/albums/anthem for The underdog/30103635.jpg--(这适用于相册,但艺术家或相册的问题相同)3)self.location不是空的。4) 媒体根仍然是一样的,我相信。注意我上面的更新。在评论中设置格式会有点棘手。你需要更多的投票。这是导致我的设置出现问题的原因。谢谢同样的问题对我来说……有趣的是,在
Nginx
之后或
manage.py runserver
的生产环境中都没有发生这种情况。但是我有额外的
/
已经好几个月了,我只是在尝试从
gunicorn
切换到
uwsgi
时才发现它,并且必须在本地测试
uwsgi
。也代表我感谢,这正是我遇到的问题!
upload_file = SimpleUploadedFile(name=basename(out_file), content=open(out_file, 'rb').read())
object = YourModel.objects.create(file=upload_file)
object = YourModel.objects.create(file=File(open(file_path, 'rb')))
djk\lib\site-packages\django\core\files\utils.py