我已经在我的settings.py中将Debug = True
更改为Debug = False
,在我的一个帖子请求中抛出了服务器错误(500),我在Google计算引擎上部署了我的Django 1.10和python 3.6项目。注册等其他帖子请求工作正常。
当我停止gunicorn进程并再次重新运行时,此POST工作几个小时并再次开始抛出服务器错误(500)。
我如何解决这个问题,因为我在计算引擎上使用Django 1.10.5,Python 3.6? 请帮帮我! 在此先感谢!
这是我的settings.py:
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '*************************'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False
ALLOWED_HOSTS = ['127.0.0.1', 'brainresearchtagging.com']
# INTERNAL_IPS = ['127.0.0.1']
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'debug_toolbar',
'users',
'article',
'import_export',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'debug_toolbar.middleware.DebugToolbarMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'brain.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_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 = 'brain.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.10/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'DB_NAME',
'USER': 'DB_USER',
'PASSWORD': 'DB_PASS',
'HOST': 'IP',
'PORT': '5432',
}
}
#Static Storage
STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'
# Password validation
# https://docs.djangoproject.com/en/1.10/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/1.10/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/1.10/howto/static-files/
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = '/assets/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'assets'), ]
# Authentication
LOGIN_URL = 'users:login'
LOGIN_REDIRECT_URL = 'users:dashboard'
EMAIL_BACKEND = 'django.core.mail.backends.filebased.EmailBackend'
EMAIL_FILE_PATH = os.path.join(BASE_DIR, 'emails')
# Django Import-Export
IMPORT_EXPORT_USE_TRANSACTIONS = True
以下是我的视图,它会引发服务器错误(5005):
来自views.py:
class TagView(LoginRequiredMixin, generic.CreateView):
form_class = forms.TagForm
def post(self, request, *args, **kwargs):
if request.method == 'POST':
post_data = request.POST.copy()
post_data.update({'user': request.user.pk})
form = forms.TagForm(post_data)
if form.is_valid():
print(form.errors)
tag = form.save(commit=False)
tag.user = request.user
tag.email = request.user.email
tag.save()
else:
return HttpResponse(form.errors, status=400)
return HttpResponseRedirect(reverse_lazy('users:dashboard'))
答案 0 :(得分:1)
如上所述,您可以在代码中加入异常处理,并将logger
放在代码中。例如:
import logging
class TagView(LoginRequiredMixin, generic.CreateView):
form_class = forms.TagForm
def post(self, request, *args, **kwargs):
try:
post_data = request.POST.copy()
post_data.update({'user': request.user.pk})
form = forms.TagForm(post_data)
if form.is_valid():
tag = form.save(commit=False)
tag.user = request.user
tag.email = request.user.email
tag.save()
else:
return HttpResponse(form.errors, status=400)
return HttpResponseRedirect(reverse_lazy('users:dashboard'))
except Exception as exp:
logging.error(exp) # For python 3
return HttpResponse(exp, status=400)
使用日志监控工具查看错误日志,如此博客提到的:https://cloud.google.com/python/monitor-and-debug/logging-application-events
有关异常处理的详细信息:https://docs.python.org/3/tutorial/errors.html
***根据答案评论中的讨论,问题出在ALLOWED_HOSTS
。在允许的主机中设置ALLOWED_HOSTS=['*']
或设置服务器的IP解决了这个问题。