Django不通过电子邮件报告内部服务器错误(HTTP状态码500) [英] Django doesn't email reporting an internal server error (HTTP status code 500)
问题描述
我可以使用以下代码发送邮件
E:\ Python \ django-test \ LYYDownloaderServer> python manage.py shellPython 3.5.2(v3.5.2:4def2a2901a5,2016年6月25日,22:01:18)[MSC v.1900 32位(在tel)]在win32上键入帮助",版权",信用"或许可证"以获取更多信息.(交互式控制台)>>>从django.core.mail导入send_mail>>>>>>发送邮件(...在这里主题",...这里是信息.",...'redstone-cold@163.com',... ['2281570025@qq.com'],... fail_silently = False,...)1个>>>
根据
出什么问题了?请帮助解决问题
settings.py
"LYYDownloaderServer项目的Django设置.由django-admin startproject使用Django 1.9.1生成.有关此文件的更多信息,请参见https://docs.djangoproject.com/zh-CN/1.9/topics/settings/有关设置及其值的完整列表,请参见https://docs.djangoproject.com/zh-CN/1.9/ref/settings/"导入操作系统ADMINS = [('Philip','r234327894@163.com'),('Philip2','768799875@qq.com']]EMAIL_HOST ='smtp.163.com'#'本地主机'#'smtp.139.com'#EMAIL_PORT = 25#EMAIL_USE_TLS =真EMAIL_HOST_USER ='r234327894@163.com'#'13529123633@139.com'EMAIL_HOST_PASSWORD ='******'#DEFAULT_FROM_EMAIL ='r234327894@163.com'#像这样在项目内部构建路径:os.path.join(BASE_DIR,...)BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__ file__)))#快速启动开发设置-不适合生产#参见https://docs.djangoproject.com/en/1.9/howto/deployment/checklist/#安全警告:对生产中使用的秘密密钥保密!SECRET_KEY ='s4(z8qzt $ = x(2t(ok5bb58_!u == + x97t0vpa = * 8bb_68baekkh''#安全警告:不要在生产中打开调试的情况下运行!调试=假ALLOWED_HOSTS = ['127.0.0.1']#,'.0letter.com'#应用定义INSTALLED_APPS = ['VideoParser.apps.VideoparserConfig','FileHost.apps.FilehostConfig','django.contrib.admin','django.contrib.auth','django.contrib.contenttypes','django.contrib.sessions','django.contrib.messages','django.contrib.staticfiles',]MIDDLEWARE_CLASSES = ['django.middleware.common.BrokenLinkEmailsMiddleware','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','django.middleware.clickjacking.XFrameOptionsMiddleware',]ROOT_URLCONF ='LYYDownloaderServer.urls'模板= [{'BACKEND':'django.template.backends.django.DjangoTemplates','DIRS':[],'APP_DIRS':是的,'选项': {'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 ='LYYDownloaderServer.wsgi.application'#数据库#https://docs.djangoproject.com/zh-CN/1.9/ref/settings/#databases数据库= {'默认': {'ENGINE':'django.db.backends.sqlite3','名称':os.path.join(BASE_DIR,'db.sqlite3'),}}#密码验证#https://docs.djangoproject.com/zh-CN/1.9/ref/settings/#auth-password-validatorsAUTH_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',},]#国际化#https://docs.djangoproject.com/zh-CN/1.9/topics/i18n/LANGUAGE_CODE ='en-us'TIME_ZONE ='UTC'USE_I18N =真USE_L10N =真USE_TZ =真#静态文件(CSS,JavaScript,图像)#https://docs.djangoproject.com/zh/1.9/howto/static-files/STATIC_URL ='/静态/'
views.py的开始
从django.http中的 导入JsonResponse,HttpResponse导入日志导入m3u8导入操作系统从VideoParser.parsers.CommonParsers导入*导入urllib.parse导入hashlib从datetime导入datetime,timedelta,日期从django.views.decorators.csrf导入csrf_exempt从django.db导入IntegrityError从VideoParser.models导入*从importlib import import_module#print('------------- views --------')FILES_DIR ='文件'#specialHostName2module = {'56':'v56'}logging.basicConfig(format ='%(asctime)s%(message)s',datefmt ='%m/%d%I:%M:%S%p',level = logging.ERROR,handlers = [logging.handlers.RotatingFileHandler(filename = os.path.join(FILES_DIR,'LYYDownloaderServer.log'),maxBytes = 1024 * 1024,backupCount = 1)])...
首先,是否可以使用控制台发送邮件并不重要,但是是否收到邮件则无关紧要.我想你做到了.
第二,为了确定,最好在控制台中尝试使用与 ADMINS
中设置的电子邮件地址完全相同的电子邮件地址.
最后,发件人地址也可能很重要.默认值为"root @ localhost",而"root"是可以的,而"localhost"则不能,某些邮件服务器可能会拒绝该电子邮件.通过设置 SERVER_EMAIL
Django设置来指定另一个发件人电子邮件地址.
I could send mail using the following code
E:\Python\django-test\LYYDownloaderServer>python manage.py shell
Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:01:18) [MSC v.1900 32 bit (In
tel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from django.core.mail import send_mail
>>>
>>> send_mail(
... 'Subject here',
... 'Here is the message.',
... 'redstone-cold@163.com',
... ['2281570025@qq.com'],
... fail_silently=False,
... )
1
>>>
According to the doc:
When DEBUG is False, Django will email the users listed in the ADMINS setting whenever your code raises an unhandled exception and results in an internal server error (HTTP status code 500). This gives the administrators immediate notification of any errors. The ADMINS will get a description of the error, a complete Python traceback, and details about the HTTP request that caused the error.
but in my case, Django doesn't email reporting an internal server error (HTTP status code 500)
what's the problem? please help fix the problem
settings.py
"""
Django settings for LYYDownloaderServer project.
Generated by 'django-admin startproject' using Django 1.9.1.
For more information on this file, see
https://docs.djangoproject.com/en/1.9/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.9/ref/settings/
"""
import os
ADMINS = [('Philip', 'r234327894@163.com'), ('Philip2', '768799875@qq.com')]
EMAIL_HOST = 'smtp.163.com' # 'localhost'#'smtp.139.com'
# EMAIL_PORT = 25
# EMAIL_USE_TLS = True
EMAIL_HOST_USER = 'r234327894@163.com' # '13529123633@139.com'
EMAIL_HOST_PASSWORD = '******'
# DEFAULT_FROM_EMAIL = 'r234327894@163.com'
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.9/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 's4(z8qzt$=x(2t(ok5bb58_!u==+x97t0vpa=*8bb_68baekkh'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False
ALLOWED_HOSTS = ['127.0.0.1']#, '.0letter.com'
# Application definition
INSTALLED_APPS = [
'VideoParser.apps.VideoparserConfig',
'FileHost.apps.FilehostConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
MIDDLEWARE_CLASSES = [
'django.middleware.common.BrokenLinkEmailsMiddleware',
'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',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'LYYDownloaderServer.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'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 = 'LYYDownloaderServer.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.9/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Password validation
# https://docs.djangoproject.com/en/1.9/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.9/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.9/howto/static-files/
STATIC_URL = '/static/'
the start of views.py
from django.http import JsonResponse, HttpResponse
import logging
import m3u8
import os
from VideoParser.parsers.CommonParsers import *
import urllib.parse
import hashlib
from datetime import datetime, timedelta, date
from django.views.decorators.csrf import csrf_exempt
from django.db import IntegrityError
from VideoParser.models import *
from importlib import import_module
# print('-------------views --------')
FILES_DIR = 'files'
# specialHostName2module = {'56': 'v56'}
logging.basicConfig(format='%(asctime)s %(message)s', datefmt='%m/%d %I:%M:%S %p', level=logging.ERROR, handlers=[logging.handlers.RotatingFileHandler(filename=os.path.join(FILES_DIR, 'LYYDownloaderServer.log'), maxBytes=1024 * 1024, backupCount=1)])
...
First, it doesn't matter if you were able to send the mail using the console, but if you received the mail. I assume you did.
Second, it's best to try with exactly the same email address in the console as the one set in the ADMINS
, just to be sure.
Finally, the sender address might also matter. The default is "root@localhost", and while "root" is OK, "localhost" is not, and some mail servers may refuse the email. Specify another sender email address by setting the SERVER_EMAIL
Django setting.
这篇关于Django不通过电子邮件报告内部服务器错误(HTTP状态码500)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!