我无法使用Python定义STATICFILES_DIRS来找出路径 [英] I can't define STATICFILES_DIRS using Python to figure out the path
问题描述
我想能够使用python定义我的静态/媒体文件设置来获取路径,所以我不需要在我的开机和我的服务器上进行不同的设置。
I want to be able to define my settings for static/media files using python to get the paths so I don't need different settings on my dev machine and my server.
所以我有这些设置;
import os
from unipath import Path
### PATH CONFIGURATION
# Absolute filesystem path to the top-level project folder
SITE_ROOT = Path(__file__).ancestor(3)
### MEDIA CONFIGURATION
MEDIA_ROOT = SITE_ROOT.child('media')
MEDIA_URL = '/media/'
### END MEDIA CONFIGURATION
### STATIC CONFIGURATION
STATIC_ROOT = SITE_ROOT.child('static')
STATIC_URL = '/static/'
# Additional locations of static files
STATICFILES_DIRS = os.path.join(SITE_ROOT, 'static'),
我的问题是,本地不会加载静态文件终端说 STATICFILES_DIRS
不应该包含 STATICFILES_ROOT
。
My problem is that locally it won't load the static files and the terminal says that STATICFILES_DIRS
should not contain the STATICFILES_ROOT
.
可以让Python加载这样的路径,还是浪费我的时间?
Is it possible to get Python to load the paths like this or am I wasting my time?
推荐答案
你的代码本身没有什么问题,这只是一个 staticfiles
应用程序是从 STATICFILES_DIRS
中指定的目录中复制文件在 STATIC_ROOT
中指定的目录中,所以在 STATIC_ROOT
目录中添加 STATICFILES_DIRS
设置。
There's nothing wrong with your code per se, it's just that the point of the staticfiles
app is to copy the files from the directories specified in STATICFILES_DIRS
into the directory specified in STATIC_ROOT
, so it doesn't make much sense to include the STATIC_ROOT
directory in the STATICFILES_DIRS
setting.
除非您实际使用 staticfiles
应用程序与 ./ manage.py collectstatic
,您也可以将 STATICFILES_DIRS
设置为空,即更改。 ..
Unless you're actually using the staticfiles
app with ./manage.py collectstatic
, you may as well just leave the STATICFILES_DIRS
setting empty, i.e. just change...
STATICFILES_DIRS = os.path.join(SITE_ROOT, 'static'),
...到...
STATICFILES_DIRS = ()
这篇关于我无法使用Python定义STATICFILES_DIRS来找出路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!