Nat*_*mes 4 django heroku django-pipeline docker whitenoise
我正在尝试使用 Docker、django-pipeline 和 Whitenoise 在 Heroku 上部署 Django 项目。容器构建成功,我看到collectstatic在container-name/static. 但是,在访问任何页面时,我都会收到以下 500 错误:
ValueError: Missing staticfiles manifest entry for 'pages/images/favicons/apple-touch-icon-57x57.png'
Run Code Online (Sandbox Code Playgroud)
下面是我的settings.py、Dockerfile、 和heroku.yml。由于我也在使用 django-pipeline,所以我不确定的一件事是使用什么设置STATICFILES_STORAGE?我试过
STATICFILES_STORAGE = 'pipeline.storage.PipelineStorage'
Run Code Online (Sandbox Code Playgroud)
但这会导致文件路径404ing。
任何建议表示赞赏。谢谢。
#settings.py
...
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = env.bool("DJANGO_DEBUG", default=False)
ALLOWED_HOSTS = ['.herokuapp.com', 'localhost', '127.0.0.1']
INSTALLED_APPS = [
"django.contrib.admin",
"django.contrib.admindocs",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
"django.contrib.sites",
# Third-party
"allauth",
"allauth.account",
"debug_toolbar",
"django_extensions",
"pipeline",
"rest_framework",
"whitenoise.runserver_nostatic",
"widget_tweaks",
# Local
...
]
MIDDLEWARE = [
"debug_toolbar.middleware.DebugToolbarMiddleware",
"django.middleware.security.SecurityMiddleware",
"whitenoise.middleware.WhiteNoiseMiddleware",
...
]
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.1/howto/static-files/
STATIC_URL = "/static/"
# STATICFILES_DIRS = [str(BASE_DIR.joinpath("code/static"))]
STATIC_ROOT = str(BASE_DIR.joinpath("static"))
MEDIA_URL = "/media/"
MEDIA_ROOT = str(BASE_DIR.joinpath("media"))
# django-pipeline config
STATICFILES_STORAGE = "whitenoise.storage.CompressedManifestStaticFilesStorage"
DEBUG_PROPAGATE_EXCEPTIONS = True
STATICFILES_FINDERS = (
"django.contrib.staticfiles.finders.FileSystemFinder",
"django.contrib.staticfiles.finders.AppDirectoriesFinder",
"pipeline.finders.PipelineFinder",
)
...
Run Code Online (Sandbox Code Playgroud)
# Dockerfile
# Pull base image
FROM python:3.8
# Set environment variables and build arguments
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
RUN curl -sL https://deb.nodesource.com/setup_12.x | bash -
RUN apt-get install -y nodejs build-essential
# Set working directory
WORKDIR /code
COPY . /code/
RUN npm install sass --dev
RUN npm install yuglify --dev
RUN npm install
RUN mkdir static
RUN mkdir staticfiles
# Install dependencies
COPY Pipfile Pipfile.lock /code/
# Figure out conditional installation of dev dependencies
# Will need to remove --dev flag for production
RUN pip install pipenv && pipenv install --system --dev
Run Code Online (Sandbox Code Playgroud)
# heroku.yml
setup:
addons:
- plan: heroku-postgresql
build:
docker:
web: Dockerfile
release:
image: web
command:
- python manage.py collectstatic --noinput
run:
web: gunicorn config.wsgi
Run Code Online (Sandbox Code Playgroud)
根据 ENDEESA 对类似 SO 帖子的回复,我将我的设置更新为以下内容,因为我的静态文件存储在其中pages/static/pages:
STATIC_URL = "/static/"
STATICFILES_DIRS = [str(BASE_DIR.joinpath("pages/static"))]
STATIC_ROOT = str(BASE_DIR.joinpath("static"))
Run Code Online (Sandbox Code Playgroud)
我还注意到我的顶级urls.py文件包含以下行:
urlpatterns += staticfiles_urlpatterns()
Run Code Online (Sandbox Code Playgroud)
据我了解,这对于在开发中提供静态文件很有用,但不应该在生产中使用,因此我将其移至仅在 DEBUG 为 True 时添加。但遗憾的是,错误仍然存在。
更神秘的是,当我跑步时
python manage.py findstatic <file-path> --verbosity 2
Run Code Online (Sandbox Code Playgroud)
找到文件:
Found 'images/favicons/apple-touch-icon-57x57.png' here:
/code/pages/static/images/favicons/apple-touch-icon-57x57.png
/code/pages/static/images/favicons/apple-touch-icon-57x57.png
Looking in the following locations:
/code/pages/static
/code/static
/usr/local/lib/python3.8/site-packages/django/contrib/admin/static
/usr/local/lib/python3.8/site-packages/debug_toolbar/static
/usr/local/lib/python3.8/site-packages/django_extensions/static
/usr/local/lib/python3.8/site-packages/rest_framework/static
Run Code Online (Sandbox Code Playgroud)
那么为什么我仍然得到ValueError: Missing staticfiles manifest entry?
最后,我得出了以下解决方案。我的主要问题是:
STATIC_ROOT我的容器中的实际目录是空的。我不知道为什么。我的解决方案是不在collectstatic中作为发布命令运行heroku.yml,而是在Dockerfile.collectstatic在我的中,Dockerfile我需要为所有环境变量设置默认值,包括我使用fromSECRET_KEY的后者。感谢Ryan Knight在这里对此进行了说明。get_random_secret_key()django.core.management.utilssettings.py需要默认密钥之外,我的最终静态文件设置如下所示。django-pipeline,我的js文件无法通过whitenoise存储选项正确加载。我最终使用了pipeline.storage.PipelineStorage。STATICFILES_DIRS根本不需要设置。之前我将其设置为:STATICFILES_DIRS = [
str(BASE_DIR.joinpath("pages/static")),
str(BASE_DIR.joinpath("staticfiles")),]
Run Code Online (Sandbox Code Playgroud)
两者都是不必要的,因为app_name/static/app_name这是Django 已经查找静态文件的默认位置,而且我实际上并没有在root/staticfiles. 所以我把这个设置去掉了。
heroku.yml我删除了 的释放命令collectstatic。DISABLE_COLLECTSTATIC,设置为1。# Dockerfile
# Pull base image
FROM python:3.8
# Set environment variables and build arguments
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
RUN curl -sL https://deb.nodesource.com/setup_12.x | bash -
RUN apt-get install -y nodejs build-essential
# Set working directory
WORKDIR /code
COPY . /code/
RUN npm install sass --dev
RUN npm install yuglify --dev
RUN npm install
# Install dependencies
COPY Pipfile Pipfile.lock /code/
RUN pip install pipenv && pipenv install --system
# Collect static files here instead of in heroku.yml so they end up in /code/static, as expected in the app
RUN python manage.py collectstatic --noinput
Run Code Online (Sandbox Code Playgroud)
# settings.py
...
from django.core.management.utils import get_random_secret_key
SECRET_KEY = env("DJANGO_SECRET_KEY", default=get_random_secret_key())
...
STATIC_URL = "/static/"
STATIC_ROOT = str(BASE_DIR.joinpath("static"))
STATICFILES_STORAGE = 'pipeline.storage.PipelineStorage'
DEBUG_PROPAGATE_EXCEPTIONS = True
STATICFILES_FINDERS = (
"django.contrib.staticfiles.finders.FileSystemFinder",
"django.contrib.staticfiles.finders.AppDirectoriesFinder",
"pipeline.finders.PipelineFinder",
)
...
Run Code Online (Sandbox Code Playgroud)
# heroku.yml
setup:
addons:
- plan: heroku-postgresql
build:
docker:
web: Dockerfile
release:
image: web
run:
web: gunicorn config.wsgi
Run Code Online (Sandbox Code Playgroud)
项目结构,如果有帮助的话:
config
settings.py
...
pages
static
pages
scss
js
images
static
Dockerfile
heroku.yml
docker-compose.yml
...
Run Code Online (Sandbox Code Playgroud)
祝其他与部署之神的战斗的人好运。愿机会永远对你有利,不要放弃!
| 归档时间: |
|
| 查看次数: |
1738 次 |
| 最近记录: |