我使用 Django 2.0 和 Heroku 来托管应用程序。
我的媒体目录设置是这样的
App/settings/production.py
:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), 'static_cdn', 'media_root')
我正在使用 gTTS 将文本转换为语音并将 .mp3
文件保存在媒体目录中:
tts_file_name = str(int(time.time())) + '.mp3'
joined_path = os.path.join(settings.MEDIA_ROOT, 'tts')
joined_path_with_file = os.path.join(joined_path, tts_file_name)
# create directory if does not exists
if not os.path.exists(joined_path):
os.makedirs(joined_path)
tts = gTTS(text='Good morning', lang='en')
tts.save(joined_path_with_file)
# tts path to send to template
tts_media_url = os.path.join(settings.MEDIA_URL, 'tts', tts_file_name)
它在本地系统上运行良好,因为我也可以手动更改文件权限。
但它不适用于 Heroku 并给出错误:
OSError: [Errno 30] Read-only file system: '/static_cdn'
我试图通过运行 heroku shell
来定位 static_cdn
,但在应用程序路径和根路径中甚至找不到 static_cdn
。但它似乎工作正常,因为其他通过表单上传的工作正常。
使用 Django 模型的 upload_to
是有效的,甚至在 static_cdn
中创建了目录。
如何在 Heroku 上的 static_cdn
中创建目录,就像 Django 使用模型的 upload_to
一样?
最佳答案
通过删除额外的 os.path.dirname()
更改了 MEDIA_ROOT
路径,它现在可以正常工作了。
MEDIA_ROOT = os.path.join(BASE_DIR, 'static_cdn', 'media_root')
关于python - 操作系统错误 : [Errno 30] Read-only file system in Django on Heroku,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50014616/