部署環(huán)境:
CentOS release 6.7 (Final)
Python 3.5.3
Django 1.11.2
開發(fā)環(huán)境靜態(tài)資源沒有出過什么問題突诬,一直健康良好的可訪問瞒窒,可在部署到Linux生產(chǎn)環(huán)境后出現(xiàn)了訪問不到的404問題湃崩。
相對(duì)比雄人,官方的指導(dǎo)還不如網(wǎng)上前輩總結(jié)的精辟到位聊记。
我的 settings.py 相關(guān)配置文件如下:
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')]
,
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.static',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.11/howto/static-files/
STATIC_URL = '/static/'
# 設(shè)置靜態(tài)文件目錄
STATIC_PATH = os.path.join(os.path.dirname(__file__), 'static')
# 生產(chǎn)環(huán)境靜態(tài)資源目錄先改,執(zhí)行 manage.py collectstatic 后靜態(tài)文件存放的路徑
STATIC_ROOT = os.path.join(BASE_DIR, 'release', 'static')
# 生產(chǎn)環(huán)境 執(zhí)行 manage.py collectstatic 后搜索配置的文件列表浩蓉,存儲(chǔ)至STATIC_ROOT目錄下
# 可以用來存儲(chǔ)公用資源派继,如: JQuery, bootstrap等
STATICFILES_DIRS = (
# os.path.join(os.path.dirname(__file__), '../static/').replace('\\', '/'),
)
# 配置項(xiàng)用來控制文件被聚集起來的方式宾袜。
# 默認(rèn)值是 django.contrib.staticfiles.storage.StaticFilesStorage ,表示將文件集合在 STATIC_ROOT 配置項(xiàng)指定的文件夾中驾窟。
# AppDirectoriesFinder 用來在每個(gè)app中的 static 文件搜索靜態(tài)文件庆猫,如: $app_name/static/ 。
# FileSystemFinder 用來搜索在 STATICFILES_DIRS 配置項(xiàng)中指定的文件夾列表中尋找靜態(tài)文件绅络。
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
# 'django.contrib.staticfiles.finders.DefaultStorageFinder',
)
配置完后在project目錄下執(zhí)行manage.py collectstatic
月培,會(huì)將靜態(tài)資源導(dǎo)入/release/static目錄下。
用uwsgi啟動(dòng)服務(wù)至后臺(tái):nohup uwsgi --http :8000 -p 4 --modul horoscope_web.wsgi &
測試:通過http://***.com/static/images/grain.png
即可訪問到images下的圖片恩急,靜態(tài)資源css, js也可以正常訪問杉畜。
但是admin后臺(tái)的靜態(tài)資源還是加載失敗,解決后補(bǔ)帖衷恭!
參考: