找不到静态文件 django 1.9 gunicorn
Not finding static files django 1.9 gunicorn
我正在部署的路上,但是当我从开发服务器切换到 gunicorn 时,找不到我的静态文件。我运行 collectstatic,文件都收集好了
请注意,在我的设置文件中,我打印了静态文件的路径。这是来自我的 gunicorn 日志:
/home/jcg/code/python/venvs/baseball/baseball_stats/collect_static
Not Found: /static/admin/css/base.css
Not Found: /static/admin/css/dashboard.css
这是我的设置文件。我已经正确地(我希望)更新了原始 django 1.6 版本的设置。
"""
Django settings for baseball_stats project.
For more information on this file, see
https://docs.djangoproject.com/en/1.6/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.6/ref/settings/
"""
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
# this for reading environment variables for private values (secret_key, etc)
# copy this to .bashrc or if using virtualvenvwrapper to bin/postactivate script
# EXPORT VARIABLE=thevaluegoeshere
import get_env_variable
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
# Most recent year for statistics
MOST_RECENT = 2013
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.6/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '-s*2+cq_niwa&*b%9(0w6$w!*9%d98oe7r6=+m0v+8(^&!10b6'
SECRET_KEY = get_env_variable.get_env_variable('SECRET_KEY')
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
TEMPLATE_DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'baseball',
'django.contrib.humanize',
'bootstrap3',
# 'debug_toolbar',
'django_extensions',
# 'csvimport',
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
ROOT_URLCONF = 'baseball_stats.urls'
WSGI_APPLICATION = 'baseball_stats.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.6/ref/settings/#databases
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Internationalization
# https://docs.djangoproject.com/en/1.6/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'America/New_York'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.6/howto/static-files/
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "collect_static")
print STATIC_ROOT
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
"""
LOGGING = {
'version': 1,
'handlers': {
'console': {
'level': 'DEBUG',
'class': 'logging.StreamHandler',
},
},
'loggers': {
'django.db.backends': {
'level': 'DEBUG',
'handlers': ['console'],
},
}
}
"""
我看了这个:Django Gunicorn not load static files
但我的理解是 gunicorn 可以处理静态文件,只是效率不如使用 nginx。我想我会在设置 nginx
之前将其作为第一步
运行 ./manage.py findstatic 找不到我的文件,所以,我猜它是我的设置文件,而不是服务器
第三方应用
您只需要 dj-static 个包裹。
pip install dj-static
在settings.py中配置静态资源:
STATIC_ROOT = 'staticfiles'
STATIC_URL = '/static/'
然后,更新您的 wsgi.py 文件以使用 dj-static:
from django.core.wsgi import get_wsgi_application
from dj_static import Cling
application = Cling(get_wsgi_application())
纯 Django
添加到您的 urls.py
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
...
urlpatterns += staticfiles_urlpatterns()
您可以从 nginx 服务器提供静态文件。在服务器设置中。
https://whosebug.com/questions/tagged/django-settings
server {
listen 80;
server_name test.com;
location / {
proxy_pass http://0.0.0.0:8000;
proxy_set_header X-Forwarded-Host $server_name;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-Ssl on;
proxy_redirect off;
}
location /static/ {
autoindex on;
alias /home/ec2-user/app/static/;
}
}
我正在部署的路上,但是当我从开发服务器切换到 gunicorn 时,找不到我的静态文件。我运行 collectstatic,文件都收集好了
请注意,在我的设置文件中,我打印了静态文件的路径。这是来自我的 gunicorn 日志:
/home/jcg/code/python/venvs/baseball/baseball_stats/collect_static
Not Found: /static/admin/css/base.css
Not Found: /static/admin/css/dashboard.css
这是我的设置文件。我已经正确地(我希望)更新了原始 django 1.6 版本的设置。
"""
Django settings for baseball_stats project.
For more information on this file, see
https://docs.djangoproject.com/en/1.6/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.6/ref/settings/
"""
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
# this for reading environment variables for private values (secret_key, etc)
# copy this to .bashrc or if using virtualvenvwrapper to bin/postactivate script
# EXPORT VARIABLE=thevaluegoeshere
import get_env_variable
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
# Most recent year for statistics
MOST_RECENT = 2013
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.6/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '-s*2+cq_niwa&*b%9(0w6$w!*9%d98oe7r6=+m0v+8(^&!10b6'
SECRET_KEY = get_env_variable.get_env_variable('SECRET_KEY')
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
TEMPLATE_DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'baseball',
'django.contrib.humanize',
'bootstrap3',
# 'debug_toolbar',
'django_extensions',
# 'csvimport',
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
ROOT_URLCONF = 'baseball_stats.urls'
WSGI_APPLICATION = 'baseball_stats.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.6/ref/settings/#databases
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Internationalization
# https://docs.djangoproject.com/en/1.6/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'America/New_York'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.6/howto/static-files/
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "collect_static")
print STATIC_ROOT
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
"""
LOGGING = {
'version': 1,
'handlers': {
'console': {
'level': 'DEBUG',
'class': 'logging.StreamHandler',
},
},
'loggers': {
'django.db.backends': {
'level': 'DEBUG',
'handlers': ['console'],
},
}
}
"""
我看了这个:Django Gunicorn not load static files
但我的理解是 gunicorn 可以处理静态文件,只是效率不如使用 nginx。我想我会在设置 nginx
之前将其作为第一步运行 ./manage.py findstatic 找不到我的文件,所以,我猜它是我的设置文件,而不是服务器
第三方应用
您只需要 dj-static 个包裹。
pip install dj-static
在settings.py中配置静态资源:
STATIC_ROOT = 'staticfiles'
STATIC_URL = '/static/'
然后,更新您的 wsgi.py 文件以使用 dj-static:
from django.core.wsgi import get_wsgi_application
from dj_static import Cling
application = Cling(get_wsgi_application())
纯 Django
添加到您的 urls.py
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
...
urlpatterns += staticfiles_urlpatterns()
您可以从 nginx 服务器提供静态文件。在服务器设置中。 https://whosebug.com/questions/tagged/django-settings
server {
listen 80;
server_name test.com;
location / {
proxy_pass http://0.0.0.0:8000;
proxy_set_header X-Forwarded-Host $server_name;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-Ssl on;
proxy_redirect off;
}
location /static/ {
autoindex on;
alias /home/ec2-user/app/static/;
}
}