部署到 Google 云 运行 后不显示静态文件
Static Files not Displayed after Deployment to Google Cloud Run
我有一个 wagtail web 应用程序,它在本地主机上运行良好,但在生产中,Debug 设置为 False,所有静态文件都没有显示在已部署的网站中,我在下面附上 Dockerfile 代码:
# Use an official Python runtime as a parent image
FROM python:3.7
LABEL maintainer="hello@wagtail.io"
# Set environment varibles
ENV PYTHONUNBUFFERED 1
ENV DJANGO_ENV production
COPY ./requirements.txt /code/requirements.txt
RUN pip install --upgrade pip
# Install any needed packages specified in requirements.txt
RUN pip install -r /code/requirements.txt
RUN pip install gunicorn
# Copy the current directory contents into the container at /code/
COPY . /code/
# Set the working directory to /code/
WORKDIR /code/
RUN python manage.py migrate
RUN useradd wagtail
RUN chown -R wagtail /code
USER wagtail
EXPOSE 8000
CMD exec gunicorn myweb_blog.wsgi:application --bind 0.0.0.0:80 --workers 3
Production.py设置文件如下:
from .base import *
SECURE_BROWSER_XSS_FILTER=True
FEATURE_POLICY = {
'geolocation': 'none',
}
#CSP_BLOCK_ALL_MIXED_CONTENT=True
#
SECURE_CONTENT_TYPE_NOSNIFF=True
SECURE_FRAME_DENY=True
SECURE_HSTS_SECONDS=2592000
SECURE_HSTS_INCLUDE_SUBDOMAINS=True
SECURE_HSTS_PRELOAD=True
X_FRAME_OPTIONS = 'DENY'
SECURE_REFERRER_POLICY='same-origin'
SECRET_KEY = 'A Secure Secret'
ALLOWED_HOSTS = ['my domain']
DEBUG = False
try:
from .local import *
except ImportError:
pass
--Base.py--
"""
Django settings for myweb_blog project.
Generated by 'django-admin startproject' using Django 3.0.4.
For more information on this file, see
https://docs.djangoproject.com/en/3.0/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.0/ref/settings/
"""
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
PROJECT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
BASE_DIR = os.path.dirname(PROJECT_DIR)
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/
# Application definition
INSTALLED_APPS = [
'home',
'search',
'blog',
'wagtail.contrib.forms',
'wagtail.contrib.redirects',
'wagtail.embeds',
'wagtail.sites',
'wagtail.users',
'wagtail.snippets',
'wagtail.documents',
'wagtail.images',
'wagtail.search',
'wagtail.admin',
'wagtail.core',
'modelcluster',
'taggit',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
MIDDLEWARE = [
'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',
'django.middleware.security.SecurityMiddleware',
'wagtail.contrib.redirects.middleware.RedirectMiddleware',
]
ROOT_URLCONF = 'myweb_blog.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
os.path.join(PROJECT_DIR, 'templates'),
],
'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 = 'myweb_blog.wsgi.application'
# Database
# https://docs.djangoproject.com/en/3.0/ref/settings/#databases
DATABASES = {
#Database details is filled here
}
# Password validation
# https://docs.djangoproject.com/en/3.0/ref/settings/#auth-password-validators
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',
},
]
# Internationalization
# https://docs.djangoproject.com/en/3.0/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.0/howto/static-files/
STATICFILES_FINDERS = [
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
]
STATICFILES_DIRS = [
os.path.join(PROJECT_DIR, 'static'),
]
# ManifestStaticFilesStorage is recommended in production, to prevent outdated
# Javascript / CSS assets being served from cache (e.g. after a Wagtail upgrade).
# See https://docs.djangoproject.com/en/3.0/ref/contrib/staticfiles/#manifeststaticfilesstorage
STATICFILES_STORAGE = 'django.contrib.staticfiles.storage.ManifestStaticFilesStorage'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
# Wagtail settings
WAGTAIL_SITE_NAME = "myweb_blog"
# Base URL to use when referring to full URLs within the Wagtail admin backend -
# e.g. in notification emails. Don't include '/admin' or a trailing slash
BASE_URL = 'my domain'
请帮我提供一个解决方案,让我的网站正常运行 运行。非常感谢
--更新--
问题已解决 感谢正确关注云 运行 文档和 White Noise(http://whitenoise.evans.io/en/stable/django.html)
Django is not built to serve static and media files in production Deploying Static Files
由于您已经在 Google 云 运行 上,解决您的问题的一个简单方法是从 Google 云选项提供静态文件,例如 Cloud Storage buckets
您可以选择在 AWS(亚马逊网络服务)上设置一个免费帐户并使用我发现更容易的 S3 存储桶。
的快速参考指南
对于静态文件,我推荐 Whitenoise。
https://github.com/GoogleCloudPlatform/django-demo-app-unicodex is an excellent starting point for running Django (and therefore Wagtail) on Google Cloud Run. It includes a section on configuring Cloud Storage Buckets.
我有一个 wagtail web 应用程序,它在本地主机上运行良好,但在生产中,Debug 设置为 False,所有静态文件都没有显示在已部署的网站中,我在下面附上 Dockerfile 代码:
# Use an official Python runtime as a parent image
FROM python:3.7
LABEL maintainer="hello@wagtail.io"
# Set environment varibles
ENV PYTHONUNBUFFERED 1
ENV DJANGO_ENV production
COPY ./requirements.txt /code/requirements.txt
RUN pip install --upgrade pip
# Install any needed packages specified in requirements.txt
RUN pip install -r /code/requirements.txt
RUN pip install gunicorn
# Copy the current directory contents into the container at /code/
COPY . /code/
# Set the working directory to /code/
WORKDIR /code/
RUN python manage.py migrate
RUN useradd wagtail
RUN chown -R wagtail /code
USER wagtail
EXPOSE 8000
CMD exec gunicorn myweb_blog.wsgi:application --bind 0.0.0.0:80 --workers 3
Production.py设置文件如下:
from .base import *
SECURE_BROWSER_XSS_FILTER=True
FEATURE_POLICY = {
'geolocation': 'none',
}
#CSP_BLOCK_ALL_MIXED_CONTENT=True
#
SECURE_CONTENT_TYPE_NOSNIFF=True
SECURE_FRAME_DENY=True
SECURE_HSTS_SECONDS=2592000
SECURE_HSTS_INCLUDE_SUBDOMAINS=True
SECURE_HSTS_PRELOAD=True
X_FRAME_OPTIONS = 'DENY'
SECURE_REFERRER_POLICY='same-origin'
SECRET_KEY = 'A Secure Secret'
ALLOWED_HOSTS = ['my domain']
DEBUG = False
try:
from .local import *
except ImportError:
pass
--Base.py--
"""
Django settings for myweb_blog project.
Generated by 'django-admin startproject' using Django 3.0.4.
For more information on this file, see
https://docs.djangoproject.com/en/3.0/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.0/ref/settings/
"""
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
PROJECT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
BASE_DIR = os.path.dirname(PROJECT_DIR)
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/
# Application definition
INSTALLED_APPS = [
'home',
'search',
'blog',
'wagtail.contrib.forms',
'wagtail.contrib.redirects',
'wagtail.embeds',
'wagtail.sites',
'wagtail.users',
'wagtail.snippets',
'wagtail.documents',
'wagtail.images',
'wagtail.search',
'wagtail.admin',
'wagtail.core',
'modelcluster',
'taggit',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
MIDDLEWARE = [
'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',
'django.middleware.security.SecurityMiddleware',
'wagtail.contrib.redirects.middleware.RedirectMiddleware',
]
ROOT_URLCONF = 'myweb_blog.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
os.path.join(PROJECT_DIR, 'templates'),
],
'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 = 'myweb_blog.wsgi.application'
# Database
# https://docs.djangoproject.com/en/3.0/ref/settings/#databases
DATABASES = {
#Database details is filled here
}
# Password validation
# https://docs.djangoproject.com/en/3.0/ref/settings/#auth-password-validators
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',
},
]
# Internationalization
# https://docs.djangoproject.com/en/3.0/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.0/howto/static-files/
STATICFILES_FINDERS = [
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
]
STATICFILES_DIRS = [
os.path.join(PROJECT_DIR, 'static'),
]
# ManifestStaticFilesStorage is recommended in production, to prevent outdated
# Javascript / CSS assets being served from cache (e.g. after a Wagtail upgrade).
# See https://docs.djangoproject.com/en/3.0/ref/contrib/staticfiles/#manifeststaticfilesstorage
STATICFILES_STORAGE = 'django.contrib.staticfiles.storage.ManifestStaticFilesStorage'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
# Wagtail settings
WAGTAIL_SITE_NAME = "myweb_blog"
# Base URL to use when referring to full URLs within the Wagtail admin backend -
# e.g. in notification emails. Don't include '/admin' or a trailing slash
BASE_URL = 'my domain'
请帮我提供一个解决方案,让我的网站正常运行 运行。非常感谢
--更新--
问题已解决 感谢正确关注云 运行 文档和 White Noise(http://whitenoise.evans.io/en/stable/django.html)
Django is not built to serve static and media files in production Deploying Static Files
由于您已经在 Google 云 运行 上,解决您的问题的一个简单方法是从 Google 云选项提供静态文件,例如 Cloud Storage buckets
您可以选择在 AWS(亚马逊网络服务)上设置一个免费帐户并使用我发现更容易的 S3 存储桶。
的快速参考指南对于静态文件,我推荐 Whitenoise。
https://github.com/GoogleCloudPlatform/django-demo-app-unicodex is an excellent starting point for running Django (and therefore Wagtail) on Google Cloud Run. It includes a section on configuring Cloud Storage Buckets.