我的目标是创建/更新在以下目录中的文件:
root/my_app_name/main/static/media/images/contact_photos
我的settings.py文件看起来是这样的:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_URL = '/static/'
if DEBUG:
MEDIA_URL = '/media/'
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR),"static","static-only")
#STATIC_ROOT = [os.path.join(BASE_DIR,"static-only")]
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR),"static","media")
#MEDIA_ROOT = [os.path.join(BASE_DIR,"media")]
STATICFILES_DIRS = (
#os.path.join(os.path.dirname(BASE_DIR),"static","static"),
os.path.join(BASE_DIR,"static"),
)
这里就是我现在做的事:
full_filename = os.path.join(settings.BASE_DIR + '\main\static\media\images\contact_photos', filename)
with open(full_filename, 'wb+') as f:
f.write(img_data)
我结束了以下错误:
[Errno 2] No such file or directory: '/root/static/media/images/file_name.jpg'
任何人都知道如何解决这个问题?我想,必须有一些joining到BASE_DIR或MEDIA_ROOT的文件路径,但我无法设法做到这一点。
+0
为什么'os.path中.join(settings.BASE_DIR +'\ main \ static \ media \ images \ contact_photos',filename)'而不是'os.path.join(settings.BASE_DIR,'\ main \ static \ media \ images \ contact_photos',filename )'? –
+0
为什么反斜杠而不是斜杠? 'os.path.join'的参数也应该是**相对**路径。 –
+0
为什么你把你的django项目直接放在'/ root'下?是否真的需要像“root/my_app_name/main/static/media/images/contact_photos”这样的深层目录结构? –