我有一個Django項目myproj
,與應用程序myapp
。在myproj/myproj
中,我有一個文件settings.py
,其中包含行STATIC_URL = '/static/'
。在myproj/myapp/static/myapp
中,我有一個名爲myfile.txt
的文件。在myproj/myapp
,我有一個文件myscript.py
,包含線路:Django:找不到文件使用靜態()
from django.templatetags.static import static
fname = static('myapp/myfile.txt')
with open(fname, 'r') as f:
# Do something
在這裏,我在瀏覽器中得到一個錯誤:No such file or directory: '/static/myapp/myfile.txt'
但是,如果我的代碼更改爲以下:
fname = 'myapp/static/myapp/myfile.txt'
with open(fname, 'r') as f:
# Do something
它工作正常。爲什麼第一個不工作?謝謝。
它看起來像'靜態的()'返回一個鏈接到資源不是它的路徑。 –
看來你需要在settings.py中正確定義你的STATIC_ROOT –