image - Pythonanywhere 将图像文件保存在一步父目录而不是正确的目录中。*开发服务器进展顺利)
问题描述
我已经制作了自定义字段来保存图像和缩略图。缩略图应保存在“/.../.../targetimage/thumbnails/”文件夹中,全尺寸图像应保存在“.../.../targetimage/”文件夹中。
当我在本地开发服务器上运行 customfield 文件时,图像进入正确的目录。但在 pythonanywhere 中,原始图像和缩略图图像进入同一目录。我不明白为什么 Pythonanywhere 的行为方式不同。
字段.py
def _add_path_to_thumb(s):
print('this is path',s)
fname_list=[]
parts = s.split(".")
print('this is parts',parts)
pathparts=parts[0].split("\\")
print('this is pathparts', pathparts)
fname_list.append(pathparts[-1])
fname_list.append('-thumb')
fname_list.append('.jpg')
fname ="".join(fname_list)
del pathparts[-1]
pathparts.extend(['thumbnails\\'])
print('this is pathparts final', pathparts)
path_prop = "\\".join(pathparts)
print('this is pathparts final prop', path_prop)
MEDIA_ROOT_THUMB = os.path.join(MEDIA_ROOT, 'target_image/thumbnails/')
print('this is media_root_thumb', MEDIA_ROOT_THUMB)
fullopathusingos = os.path.join(MEDIA_ROOT_THUMB,fname)
print('this is full path using os ',fullopathusingos )
fullpath = path_prop+fname
return fullopathusingos
解决方案
You're switching between using \ and / as path separators. It will behave one way on Windows and another way on PythonAnywhere. I suspect the main cause of the problem is splitting on \ because, on PythonAnywhere, you will not get a path where splitting on \ breaks it into directory parts.
推荐阅读
- javascript - 未收到发送到“/topics/all”的 FCM
- android-layout - 如何将 ViewGroup 转换为白色背景位图?
- spring-boot - Spring Cloud Config Server:GitLab 凭据被暴露
- c# - 使用 LINQ 搜索多个术语
- python - 配对三个不同的列表
- node.js - 如何修改vuejs项目的语言数据?
- javascript - 在使用过滤器之前如何使数据表为空?
- r - 如何在ggplot的条形图上注释某个点?
- android - 如何在运行时更改 android:bottom?
- javascript - 从Array动态创建变量到Ajax Success