问题描述
在我的设置debug=False
之后。来自数据库的图像未显示出来,否则显示出来。静态文件显示的很好。甚至我的媒体文件都出现在debug=False
之前。数据库具有正确的文件地址,但没有显示出来。
以下是代码,我如何访问封面图像。
<div class="product-top">
<img src="{{ product.cover.url }}" alt="book-image">
<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为false时django本身不会提供静态文件和媒体文件,如果您正在生产中,则应使用ha代理或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;
}
}