合并路径(相对路径)位于基本路径组件(完整路径)之外 [英] The joined path (relative path) is located outside of the base path component (full path)

查看:39
本文介绍了合并路径(相对路径)位于基本路径组件(完整路径)之外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用缩略图包从基本图像生成缩略图.最初,我将源图像保存在静态目录中,并且由于缩略图包希望将其生成到媒体目录中,因此我认为这是导致SuspiciousFileOperation错误的原因.

I am trying to use thumbnail packages to generate thumbnail images from base images. Originally, I had my source images in my static dir, and as the thumbnail packages want to generate them to my media dir, I thought that was the cause of the SuspiciousFileOperation error I was getting.

没问题,我只是将图像复制到媒体目录中,我认为这可以解决问题,但是不,问题仍然存在.

No problem, I just copied my images to my media dir, which I thought would solve the issue, but no, the issue remains.

据我所知,相对路径与完整路径似乎是一个问题?

From what I can tell, it seems to be a problem with having a relative path vs a full path?

完整错误是:

SuspiciousFileOperation at /toys/
The joined path (/media/images/test.jpg) is located outside of the base path component (/home/username/django/first_webapp/my_site/media)

路径/home/username/django/first_webapp/my_site/media/images/test.jpg是有效的,而test.jpg是有效的jpg图像.

The path /home/username/django/first_webapp/my_site/media/images/test.jpg is valid and test.jpg is a valid jpg image.

我在模板中使用的带有sorl-thumbnail的简化代码(尽管我也尝试过easy_thumbnails):

The abridged code I am using in my template, with sorl-thumbnail (although I have also tried with easy_thumbnails) is:

{% for instance in prods %}
    <img src=" {% thumbnail instance.image_url 300x300 %} "> 
{% endfor %}

在这种情况下,

instance.image_url设置为/media/images/test.jpg

我的settings.py中的我的媒体目录设置

My media directory settings from my settings.py

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'

我不确定从哪里开始进行故障排除.

I am unsure where to begin to troubleshoot this.

推荐答案

我真的无法理解您如何认为字段类型不相关.当然是.

I can't really understand how you think the type of the field is not relevant. Of course it is.

在您的情况下,您(出于某种原因)使用了TextField来存储图像文件的路径.TextField对媒体文件没有特殊知识,sort-thumbnail只是将内容视为路径组件,然后将其与MEDIA_ROOT结合在一起.但是,由于您的路径以斜杠开头,因此 os.path.join(MEDIA_ROOT,path)的结果只是 path ;前导斜杠表示从文件系统根目录开始".因此,结果是在项目外部创建了一条路径,出于安全原因,Django禁止使用该路径.

In your case, you have (for some reason) used a TextField to store the path of your image file. TextFields have no special knowledge of media files, and sort-thumbnail just treats the contents as a path component, which it then joins with MEDIA_ROOT. But since your path begins with a leading slash, the result of os.path.join(MEDIA_ROOT, path) is just path; the leading slash means exactly "start from the filesystem root". So the result is a path outside your project, which Django disallows for security reasons.

一个快速的解决方法是删除前导斜线-以及重复的"media"前缀-并仅存储"images/test.jpg".但是真正的解决方法是对要存储的内容使用适当的字段,然后让该字段为您管理.

A quick fix is to remove the leading slash - as well as the duplicate "media" prefix - and just store "images/test.jpg". But the real fix is to use the appropriate field for the content you are storing, and let that field manage it for you.

这篇关于合并路径(相对路径)位于基本路径组件(完整路径)之外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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