Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
137 views
in Technique[技术] by (71.8m points)

python - Static Root and Static Url confusion in Django

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

code:

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'

code:

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()

my settings.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 ?

Thank you.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

From the django docs,

STATIC_ROOT is the absolute path to the directory where collectstatic will collect static files for deployment.

STATIC_URL is the URL to use when referring to static files located in STATIC_ROOT.

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/'

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/')

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...