Skip to main content
 首页 » 编程设计

python之可疑文件操作..加入的路径...位于基本路径组件之外

2024年10月24日9lyj

尝试将 .png 文件上传到管理图片表时出现错误:

SuspiciousFileOperation at /admin/products/productimage/add/ The joined path (/product/images/Laptop-Computer-5_copy.png) is located outside of the base path component (/images)

这是我的设置:

  import os 
 
 
 
# BASE_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__))) 
 
'''path settings for django==1.8''' 
 
BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(__file__))) 
 
TEMPLATE_DIRS = ( 
    #'/Users/jmitch/Desktop/seven/static/templates/', 
    os.path.join(os.path.dirname(os.path.dirname(BASE_DIR)), "static", "templates"), 
) 
 
# STATICFILES_FINDERS = ( 
#     'django.contrib.staticfiles.finders.FileSystemFinder', 
#     'django.contrib.staticfiles.finders.AppDirectoriesFinder', 
#     #'django.contrib.staticfiles.finders.DefaultStorageFinder', 
# ) 
 
TEMPLATES = [ 
        { 
            'BACKEND': 'django.template.backends.django.DjangoTemplates', 
            # 'DIRS': [BASE_DIR+"/templates", ], 
            'DIRS': [BASE_DIR + "/static/templates/", ], 
            'APP_DIRS': True, 
            'OPTIONS': { 
                'context_processors': [ 
                    'django.template.context_processors.debug', 
                    'django.template.context_processors.request', 
                    'django.contrib.auth.context_processors.auth', 
                    'django.contrib.messages.context_processors.messages', 
                    'django.core.context_processors.media', 
                    'django.template.context_processors.media', 
                ], 
            }, 
        }, 
    ] 
ENV_PATH = os.path.dirname(os.path.dirname(os.path.dirname(__file__))) 
MEDIA_ROOT = os.path.join(ENV_PATH, '/images') 
# MEDIA_ROOT = os.path.join(os.path.dirname(__file__), 'static', 'media') 
# MEDIA_ROOT = '/Volumes/Storage/_codework/e_shop/static/media/product/images/' 
 
 
STATIC_ROOT = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(__file__))), 'static', 'static-only') 
 
STATIC_URL = '/static/' 
 
STATICFILES_DIRS = ( 
    os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(__file__))), 'static', 'static'), 
) 
# MEDIA_ROOT = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(BASE_DIR))), "static", "media") 
MEDIA_URL = '/media/' 
enter code here 

但是当我在 MEDIA_ROOT 中添加“product/images”(在“product”之前没有斜线) 发生错误:

SuspiciousFileOperation at /admin/products/productimage/add/ The joined path (/product/images/Laptop-Computer-5_copy.png) is located outside of the base path component (/Volumes/Storage/_codework/e_shop/product/images)

虽然出于测试目的,我将 product/images 文件夹的副本放在项目的根目录中。否则,当斜杠在 product('/product/images') 之前时,我会遇到该错误:

OSError at /admin/products/productimage/add/[Errno 13] Permission denied: '/product'

我已经更改了访问权限,例如 (MacOs):

chmod -R 777 /Volumes/Storage/_codework/e_shop/static/media/

和

chown -R K /Volumes/Storage/_codework/e_shop/static/media/

但没有任何帮助。 什么问题?请指教。这个问题和我之前的问题有关please see

请您参考如下方法:

您进行了一些更改,但这些更改未与数据库同步。我在实现级别上搜索过它,这不是错误。确保您使用的是更新版本的 Pillow 和 image = models.ImageField(upload_to="whateverfolder/"。如果您使用的是 1.8 及以上版本,则使用“python manage.py makemigrations”进行同步。如果您使用的是 1.8 以下版本,请执行“python manage.py syncdb 你的问题就解决了。 为了将来养成同步模型的习惯。