Django中的静态根和静态URL混淆 [英] Static Root and Static Url confusion in Django
问题描述
我正在尝试在Django中读取创建的mp3文件。但是我对配置的static和static_root感到困惑。
发生的事情是,在我的代码中,当我打印以下行时,它显示了
/usr/local/src/mena_recording/play/static/audio/dorris_0_.mp3
I am trying to read create mp3 files in django. but I am confused about static and static_root that I have configured.
WHat happening is that in my code at a point when I print the below line it shows
/usr/local/src/mena_recording/play/static/audio/dorris_0_.mp3
代码:
print settings.BASE_DIR+'/play/static/audio/'+record.driverName +'_'+str(counter)+'_'+ '.mp3'
,但是当我在本文的下一行中使用相同的内容时,会出现此错误:
but when I use the same thing in the next line in this piece it gives this error:
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.
)
有人能启发我吗,这是怎么回事?
Would someone enlighten me please how this works ?
谢谢。
推荐答案
从Django文档中,
From the django docs,
STATIC_ROOT
是collectstatic将收集用于部署的静态文件的目录的绝对路径。
STATIC_ROOT
is the absolute path to the directory where collectstatic will collect static files for deployment.
STATIC_URL
是引用位于 STATIC_ROOT中的静态文件时要使用的URL
。
因此,当您请求某些特定的静态资源时,将在 STATIC_ROOT + STATIC_URL $ c中进行搜索$ c>,然后再送达。
So, when you request some specific static resource, it is searched in STATIC_ROOT + STATIC_URL
and then served.
现在遇到了问题,您可以这样做
Now in your problem, you do
STATIC_ROOT = os.path.join(BASE_DIR, 'play/static_root')
STATIC_URL = '/static/'
这意味着django实际上已经是大海在 BASE_DIR / play / static_root / static /
中运行是不正确的,因此查看其他路径,您可以知道您需要这样做
which means django would have effectively been searching in BASE_DIR/play/static_root/static/
which would be incorrect, so looking at other paths you can figure out that you need to do
STATIC_ROOT = os.path.join(BASE_DIR, 'play/')
这篇关于Django中的静态根和静态URL混淆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!