Djang中的静态根和静态Url混淆

2024-10-04 01:33:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图阅读在django创建mp3文件。但是我对我配置的静态根和静态根感到困惑。 在我的代码中,当我打印下一行时,它显示
/usr/local/src/mena_录制/播放/静态/音频/dorris_0_0.mp3

代码:

print settings.BASE_DIR+'/play/static/audio/'+record.driverName +'_'+str(counter)+'_'+ '.mp3'

但当我在这篇文章的下一行使用相同的东西时,它会给出这个错误:

IOError at /
[Errno 2] No such file or directory: u'/usr/local/src/mena_recording/play/static_root/play/static/audio/dorris_0_.oga'

代码:

with open(settings.BASE_DIR+'/play/static/audio/'+record.driverName +'_'+str(counter)+'_'+ '.mp3', 'w') as mp3_file:
    mp3_file.write(decoded_mp3_str)
    mp3_file.close()

我的设置.py

STATIC_ROOT = os.path.join(BASE_DIR, 'play/static_root')
STATIC_URL = '/static/'

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'mena_recording/static'),
    os.path.join(BASE_DIR, 'play/static'),
    # Put strings here, like "/home/html/static" or "C:/www/django/static".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
)

有人能告诉我这是怎么回事吗?

谢谢你。


Tags: pathdjango代码playbaseosdir静态
1条回答
网友
1楼 · 发布于 2024-10-04 01:33:15

从django医生那里

^{}是collectstatic将收集静态文件以进行部署的目录的绝对路径。

^{}是引用位于STATIC_ROOT中的静态文件时要使用的URL。

因此,当您请求某个特定的静态资源时,它会在STATIC_ROOT + STATIC_URL中搜索,然后被服务。

现在你的问题是

STATIC_ROOT = os.path.join(BASE_DIR, 'play/static_root')
STATIC_URL = '/static/'

这意味着django将有效地在BASE_DIR/play/static_root/static/中进行搜索,这将是不正确的,因此查看其他路径可以发现您需要执行的操作

STATIC_ROOT = os.path.join(BASE_DIR, 'play/')

相关问题 更多 >