Django3.0:来自数据库的图像在 debug=False 后未显示
Posted
技术标签:
【中文标题】Django3.0:来自数据库的图像在 debug=False 后未显示【英文标题】:Django3.0: Images from database not showing-up after debug=False 【发布时间】:2020-12-12 11:29:15 【问题描述】:在我的设置中debug=False
之后。数据库中的图像没有显示,否则它们会显示。静态文件显示得非常好。甚至我的媒体文件也出现在debug=False
之前。数据库有正确的文件地址,但覆盖不显示。
以下是代码,我如何访问封面图片。
<div class="product-top">
<img src=" product.cover.url " >
<h5> product.title </h5>
</div>
我的settings.py相关代码:
import os
import django_heroku
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
TEMP_DIR = os.path.join(BASE_DIR, 'templates')
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'abc'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False
ALLOWED_HOSTS = ['*']
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'abc@gmail.com'
EMAIL_HOST_PASSWORD = 'xyz'
EMAIL_PORT = 25
EMAIL_USE_TLS = True
DEFAULT_FROM_EMAIL = 'abc@gmail.com'
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
BASE_URL = '127.0.0.1:8000'
MANAGERS = (
('abc', "abc@gmail.com"),
)
ADMINS = MANAGERS
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'debug_toolbar',
'crispy_forms',
# myapps
'myapp',
]
AUTH_USER_MODEL = 'accounts.User'
STRIPE_SECRET_KEY = 'abc'
STRIPE_PUB_KEY = 'abc'
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'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',
'debug_toolbar.middleware.DebugToolbarMiddleware',
# Simplified static file serving.
'whitenoise.middleware.WhiteNoiseMiddleware',
]
ROOT_URLCONF = 'project.urls'
TEMPLATES = [
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [TEMP_DIR]
,
'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 = 'project.wsgi.application'
# Database
# https://docs.djangoproject.com/en/3.0/ref/settings/#databases
DATABASES =
'default':
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
## Password hashing (included)
PASSWORD_HASHERS = [
'django.contrib.auth.hashers.PBKDF2PasswordHasher',
'django.contrib.auth.hashers.PBKDF2SHA1PasswordHasher',
'django.contrib.auth.hashers.BCryptSHA256PasswordHasher',
]
# 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',
,
]
# 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/
STATIC_URL = '/static/'
STATIC_br = os.path.join(BASE_DIR,
'bookrepo/static')
STATIC_seller = os.path.join(BASE_DIR, 'seller/static')
STATICFILES_DIRS = [
STATIC_br, STATIC_seller
]
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles/')
# MEDIA INFORMATION:
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
正如我的terminal
在debug=false
之后所说:
[23/Aug/2020 23:07:00] "GET /media/covers/product1.PNG HTTP/1.1" 404 11974
正如我的terminal
在debug=true
之后所说:
[23/Aug/2020 23:25:15] "GET /media/covers/product1.PNG HTTP/1.1" 200 103898
【问题讨论】:
当使用带有DEBUG=True
url 模式的runserver 来为您的静态和媒体文件提供服务时。要在生产环境中提供文件,您需要配置网络服务器以提供这些目录
@lain 如果您能用一些相关代码告诉我出路,请多多关照。我什至部署到 heroku,静态文件也显示在那里,但没有媒体文件。
这与代码无关,更多与配置部署有关,请参阅静态文件的 Django 文档(这也适用于使用基于文件的后端时的媒体文件)docs.djangoproject.com/en/3.1/howto/static-files/deployment
【参考方案1】:
值得注意的是,当 debug 为 false 时,django 本身不提供静态和媒体文件。如果您在生产中,则应使用反向代理,如 ha proxy 或 nginx,否则将 debug 设置为 true 以进行本地开发 下面是为您的应用程序、静态和媒体文件提供服务的 nginx 服务器块的示例。
server
server_name your_server_ip;
proxy_read_timeout 600s;
client_max_body_size 25M;
location /static
alias /home/trello/static/;
location /media
alias /home/trello/media/;
location /
proxy_pass http://127.0.0.1:8000;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_redirect off;
【讨论】:
对不起,我是初学者。那么,请您告诉我在哪里以及如何将此块用于本地服务器http://127.0.0.1:8000
。谢谢
如果你在 Linux 上,你应该安装 nginx 然后在 'etc/nginx/sites-available/' 创建配置脚本 'your_configuration.conf' 然后添加你的块并运行命令:'ln -s /etc/nginx/sites-available/reverse-proxy.conf /etc/nginx/sites-enabled/your_configuration.conf' 符号链接然后最后'nginx -t' 来测试配置以上是关于Django3.0:来自数据库的图像在 debug=False 后未显示的主要内容,如果未能解决你的问题,请参考以下文章