可疑操作Django [英] Suspicious Operation Django

查看:391
本文介绍了可疑操作Django的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图删除上传的图片时遇到问题。

I've been running into a problem while trying to delete uploaded images.

错误是这样的:

SuspiciousOperation: Attempted access to '/media/artists/12-stones/154339.jpg' denied.

阅读后看起来像是错误是由于它正在寻找图像

After reading around it looks like the error is due to the fact that it's looking for the image in the wrong place (notice first slash, /media/ doesn't exist on the filesystem)

我的MEDIA_ROOT和MEDIA_URL是:

My MEDIA_ROOT and MEDIA_URL are:

MEDIA_ROOT = '/home/tsoporan/site/media/'
MEDIA_URL = "/media/

我的模型upload_to参数传递此函数:

My models upload_to parameter is passed this function:

def get_artist_path(instance, filename):
  return os.path.join('artists', slugify(instance.name), filename)

我的问题是:

1)如何解决这个问题,以备将来上传?

1) How can I fix this problem for future uploads?

2)是否可以修复我当前图像的路径而不必重新上传?

2) Is it possible to fix my current images' paths without having to reupload?

问候,
提图斯

Regards, Titus

推荐答案

嗯,代码中的一点点说明可能会有更深层的错误信息在pjango / core / files / storage.py,第210行(这是1.1.1)中,我们有:

Well, a little grepping around in the code shows that there may be a deeper error message that got homogenized along the way.



in django/core/files/storage.py, line 210 (this is in 1.1.1) we have:

def path(self, name):
    try:
        path = safe_join(self.location, name)
    except ValueError:
        raise SuspiciousOperation("Attempted access to '%s' denied." % name)
    return smart_str(os.path.normpath(path))

所以错误必须从safe_join()中出来。

So the error has to be coming out of safe_join().

django / utils / _os.py,我们有以下。请注意它在第三个到最后一行的ValueError:

In django/utils/_os.py, we have the following. Note the ValueError it throws on the third to last line:

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

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

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

==嗯,连接的路径位于基本路径组件之外。

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

嗯,现在有几个对abspathu()的调用(其定义在这个例程之上,对于NT而言与其他操作系统不同)。 abspathu()将所有非绝对路径转换为绝对路径,依靠os.cwdu(),即当前工作目录。

Hmmm, "The joined path is located outside of the base path component". Now there are a couple of calls to abspathu() in there (which is defined just above this routine and is different for NT than for other OSes). abspathu() converts all non-absolute paths to absolute by tacking on os.cwdu(), the current working directory.

问题:任何机会,你有一个符号链接(符号链接)到您的媒体目录?换句话说,它不是项目目录的直接孩子?我不知道这是否是一个有效的问题,它只是弹出我的头。

Question: By any chance do you have a symlink (symbolic link) to your media directory? In other words, it's not a direct child of the project directory? I don't know if this is a valid question, it just popped out of my head.

问题: code> self.location 和名称正在传递给safe_join()?

Question: What are the values of self.location and name that are being passed to safe_join()?

Wild-ass猜测:是 self.location empty?

Wild-ass-guess: is self.location empty?

另一个野蛮猜测:MEDIA_ROOT以某种方式变为 / media /

Another wild-ass-guess: did MEDIA_ROOT somehow get changed to /media/?

如果您有自己的Django副本(它是不难做),尝试在这些例程中放置一些print语句,然后将其作为开发服务器运行。打印输出将进入控制台。

If you have your own copy of Django installed (it's not hard to do), trying putting some print statements in these routines and then run it as the development server. The print output will go to the console.

更新:嗯。你说2)self.location和name的值是:/ home / tsoporan / site / media和/media/albums/anthem-for-the-underdog/30103635.jpg

Update: Hmmm. You said "2) The values for self.location and name are: /home/tsoporan/site/media and /media/albums/anthem-for-the-underdog/30103635.jpg"

以下路径是否有意义?

Does the following path make any sense?

"/home/tsoporan/site/media/media/albums/anthem-for-the-underdog"

请注意... / media / media / ...。

Note the .../media/media/... in there.

另外,这是什么操作系统? Django rev?

Also, what OS is this? Django rev?

这篇关于可疑操作Django的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆