静态文件找不到 - 在Heroku上部署Django [英] Static files not found - Deploying Django on Heroku
问题描述
我试图在Heroku上部署一个Django站点,但遇到问题,让应用程序找到我的静态文件。我已经使用 python manage.py collectstatic
将我的静态文件收集到一个staticfiles文件夹中,但是我的应用程序似乎还没有找到它们。我继续在我的日志中收到这样的错误:
我我不知道我是否正确地引用路径。在代码中设置为图像/样式表/脚本的路径正在使用开发中使用的原始静态文件夹的路径。我必须重写所有这些路径才能指向使用 collectstatic
命令创建的新的staticfiles文件夹,还是有其他可能导致此问题的问题? p>
我的settings.py看起来像这样:
import os
BASE_DIR = os.path.dirname(os.path.dirname(__ file__))
DEBUG = False
ALLOWED_HOSTS = ['www.tomdeldridge.com ']
INSTALLED_APPS =(
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
' django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware
ROOT_URLCONF ='tomdeldridge.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS':['tomdeldridge / templates / tomdeldridge /'],
'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',
],
},
},
]
WSGI_APPLICA TION ='tomdeldridge.wsgi.application'
DATABASES = {
'default':{
'ENGINE':'django.db.backends.sqlite3',
'NAME':os.path.join(BASE_DIR,'db.sqlite3'),
}
}
LANGUAGE_CODE ='en-us'
TIME_ZONE ='UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
STATICFILES_DIRS =(
os.path.join(
os.path.dirname(__ file__),
'static',
),
)
STATIC_ROOT ='staticfiles'
STATIC_URL ='/ static /'
我的wsgi.py文件:
import os
from django.core.wsgi import get_wsgi_application
from dj_static import Cling
application = Cling(get_wsgi_application ())
os.environ.setdefault(DJANGO_SETTINGS_MODULE,tomdeldridge.settings)
我的目录结构:
我在模板中试图引用的图像绝对存在(当我在本地运行应用程序时,它们工作正常。)我这样引用:
{%static'tomdeldridge / images / computer-2.png'%}
我必须使用像nginx这样的服务器在部署过程中提供静态文件吗?我完全失去了从这里去哪里,我不知道为什么有必要重新配置整个静态文件结构才能部署。
安装 dj-static
package
$ pip install dj-static
在 settings.py
中配置静态资产:
DEBUG = False
BASE_DIR = os.path.dirname(os.path.dirname(__ file__))
STATICFILES_DIRS =(
os.path.join(BASE_DIR,'static'),
)
STATIC_ROOT ='staticfiles'
STATIC_URL ='/ static /'
然后,更新您的 wsgi.py
文件以使用dj-static:
import os
os.environ.setdefault(DJANGO_SETTINGS_MODULE,tomdeldridge.settings)
from django.core.wsgi import get_wsgi_application
from dj_static import Cling
application = Cling(get_wsgi_application())
I am trying to deploy a Django site on Heroku, but I'm running into problems getting the app to locate my static files. I have used python manage.py collectstatic
to collect my static files into a staticfiles folder, but my app still doesn't seem to be able to find them. I continue to get errors like this in my log:
I'm not sure if I am referencing the paths properly. The path's that are set to images/stylesheets/scripts in the code are using the path to the original static folder used in development. Do I have to rewrite all of those paths to point to the new staticfiles folder I created with the collectstatic
command, or is there some other issue that could be causing this?
My settings.py looks like this:
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
DEBUG = False
ALLOWED_HOSTS = ['www.tomdeldridge.com']
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware',
)
ROOT_URLCONF = 'tomdeldridge.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ['tomdeldridge/templates/tomdeldridge/'],
'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',
],
},
},
]
WSGI_APPLICATION = 'tomdeldridge.wsgi.application'
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
STATICFILES_DIRS = (
os.path.join(
os.path.dirname(__file__),
'static',
),
)
STATIC_ROOT = 'staticfiles'
STATIC_URL = '/static/'
My wsgi.py file:
import os
from django.core.wsgi import get_wsgi_application
from dj_static import Cling
application = Cling(get_wsgi_application())
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "tomdeldridge.settings")
My directory structure:
The images I'm trying to reference in my template definitely exist (they work fine when I run the app locally.) I reference them like this:
{% static 'tomdeldridge/images/computer-2.png' %}
Do I have to use a server like nginx to serve static files in deployment? I am totally lost on where to go from here, and I'm not really sure why it's necessary to reconfigure the entire static file structure just to deploy.
Install dj-static
package
$ pip install dj-static
Configure your static assets in settings.py
:
DEBUG = False
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
STATIC_ROOT = 'staticfiles'
STATIC_URL = '/static/'
Then, update your wsgi.py
file to use dj-static:
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "tomdeldridge.settings")
from django.core.wsgi import get_wsgi_application
from dj_static import Cling
application = Cling(get_wsgi_application())
这篇关于静态文件找不到 - 在Heroku上部署Django的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!