簡體   English   中英

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

碼:

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

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

請有人賜教,請問這是如何工作的?

謝謝。

來自django docs,

STATIC_ROOT是collectstatic將收集靜態文件以進行部署的目錄的絕對路徑。

STATIC_URL是引用位於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/')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM