[英]Django | Local Bad Request (400), DEBUG=False, ALLOWED_HOSTS = ['*'], whitenoise enabled
I'm trying to solve that Bad Request (400) for 3 days now... I try to deploy my first Django App, that works perfectly with DEBUG=True. 我现在想解决3天的Bad Request(400)问题...我尝试部署我的第一个Django App,它与DEBUG = True完美结合。
After set DEBUG to False, my troubles comes. 将DEBUG设置为False后,麻烦就来了。 Notice that I work locally and I use "whitenoise" to deserve images through the "staticfiles" dir, and it works nicelly (after that I want to host it on Heroku).
请注意,我在本地工作,并且使用“ whitenoise”通过“ staticfiles”目录保存图像,并且效果很好(此后,我希望将其托管在Heroku上)。
The strange thing is that I can acces on one URL correctly of my site and the admin without any problems.... 奇怪的是,我可以正确访问我的网站和管理员的一个URL,而不会出现任何问题。
I don't know where to search anymore... And the log said nothing that could help me, just : 我不知道该在哪里搜索了……日志上没有任何内容可以帮助我,只是:
[15/May/2016 16:28:35] "GET /page/news HTTP/1.1" 400 26
import os
import dj_database_url
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
print("BASE_DIR = ",BASE_DIR)
MEDIA_ROOT = os.path.join(BASE_DIR, 'wt/static/media/')
MEDIA_URL = '/media/'
SECRET_KEY = 'H-E-R-E__I-S__M-Y__K-E-Y'
DEBUG = False
INSTALLED_APPS = [
'ckeditor',
'ckeditor_uploader',
'team.apps.TeamConfig',
'gallery.apps.GalleryConfig',
'shop.apps.ShopConfig',
'events.apps.EventsConfig',
'blog.apps.BlogConfig',
'paginator.apps.paginatorConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
MIDDLEWARE_CLASSES = [
'django.middleware.security.SecurityMiddleware',
'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',
]
ROOT_URLCONF = 'wt.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
"django.contrib.auth.context_processors.auth",
"django.core.context_processors.request",
"django.core.context_processors.debug",
"django.core.context_processors.i18n",
"django.core.context_processors.media",
"django.core.context_processors.static",
"django.core.context_processors.tz",
"django.contrib.messages.context_processors.messages",
],
},
},
]
WSGI_APPLICATION = 'wt.wsgi.application'
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'wt_db',
'USER': 'postgres',
'PASSWORD': 'H-E-R-E__I-S__M-Y__P-W',
'HOST': '127.0.0.1',
'PORT': '5432',
}
}
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',
},
]
LANGUAGE_CODE = 'fr-fr'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
db_from_env = dj_database_url.config(conn_max_age=500)
DATABASES['default'].update(db_from_env)
ALLOWED_HOSTS = ['localhost', '127.0.0.1',]
STATIC_ROOT = os.path.join(BASE_DIR, 'wt/staticfiles')
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'wt/static'),
]
STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'
CKEDITOR_UPLOAD_PATH = 'uploads'
CKEDITOR_IMAGE_BACKEND = 'pillow'
CKEDITOR_BROWSE_SHOW_DIRS = True
from django.core.wsgi import get_wsgi_application
from whitenoise.django import DjangoWhiteNoise
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "wt.settings")
application = get_wsgi_application()
application = DjangoWhiteNoise(application)
My head is burning, thanks a lot for helping ! 我的头在燃烧,非常感谢您的帮助!
I gess it was a security issue. 我认为这是一个安全问题。 Even if "whitenoise" is good to serve true
static
files in production, it can't serve media
files. 即使“ whitenoise”可以很好地在生产环境中提供真正的
static
文件,也不能提供media
文件。
I was making a structure error : 我犯了一个结构错误:
# Don't place your 'media' files IN your 'static' file like this :
MEDIA_ROOT = os.path.join(BASE_DIR, 'wt/static/media/')
MEDIA_ROOT never have to be in the "static" file of your project (even if you can make it works in some ways, it's not a good practice I think). MEDIA_ROOT永远不必放在项目的“静态”文件中(即使您可以使它以某种方式工作,但我认为这不是一个好习惯)。
'MEDIA' files (in production), have to be serve out of the Django project. (生产中的) “ MEDIA”文件必须在Django项目之外提供。 I've read somewhere that we have to use a CDN.
我读过某个地方,我们必须使用CDN。 And firstly I choose CloudFlare (because it's free), but it wasn't working, cause you need a subdomain/hostname to point your MEDIA_ROOT, and CloudFlare doesn't give that.
首先,我选择CloudFlare(因为它是免费的),但是它无法正常工作,因为您需要一个子域/主机名来指向MEDIA_ROOT,而CloudFlare却没有提供。 Finaly I choose Amazon S3.
最后,我选择了Amazon S3。
So, in conclusion write something like {% static img.thumbnail.url %}
make no sense. 因此,总而言之,编写类似
{% static img.thumbnail.url %}
是没有意义的。 Because everything uploaded via admin/user haven't to be in "static". 因为通过管理员/用户上传的所有内容都不必处于“静态”状态。
Use {{ img.thumbnail.url }}
instead. 请改用
{{ img.thumbnail.url }}
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.