Static Confusion and Static URL in Django

I am trying to read creating mp3 files in django. but I am confused by the static and static_root that I configured. What happens in my code the moment I print the line below, 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 in the next line in this fragment, 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 .py settings

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

Will someone enlighten me, please, how does it work?

Thanks.

+6
source share
1 answer

From django docs,

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

STATIC_URL is the URL used to access static files located in STATIC_ROOT .

So, when you request a specific static resource, it is executed in STATIC_ROOT + STATIC_URL and then serviced.

Now in your problem you do

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

which means that django would effectively look in BASE_DIR/play/static_root/static/ which would be wrong, so looking at other paths you can understand what you need to do

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

Source: https://habr.com/ru/post/974263/


All Articles