Django不会通过电子邮件报告内部服务器错误(HTTP状态代码500)

2024-09-30 01:24:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我可以用下面的代码发送邮件

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
>>> 

根据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.

但在我的例子中,Django不会通过电子邮件报告内部服务器错误(HTTP状态代码500) enter image description here

怎么了?请帮忙解决这个问题

在设置.py在

^{pr2}$

开始视图.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)])
...

Tags: thedjango代码frompyimportcomsend
3条回答

首先,您是否能够使用控制台发送邮件并不重要,但是您是否收到了邮件。我想是你干的。在

其次,最好在控制台中使用与ADMINS中设置的电子邮件地址完全相同的电子邮件地址,只是为了确定。在

最后,发件人地址可能也很重要。默认值为“root@localhost,而“root”可以,而localhost则不行,一些邮件服务器可能会拒绝该电子邮件。通过设置SERVER_EMAILDjango设置指定另一个发件人电子邮件地址。在

Djano使用日志系统发送错误的管理电子邮件。在

正如我从您的views.py中看到的,您正在更改日志记录设置。 这可能是您清除django管理处理程序mail_admins时出现问题的原因。在

有关详细信息,请查看django documentation

Django doc表示:为了发送电子邮件,电子邮件主机、电子邮件主机用户和电子邮件主机密码是最不需要的,但正如我所测试的,我们还应该指定服务器电子邮件,并且只有当服务器电子邮件等于email主机用户时才能发送电子邮件,例如

EMAIL_HOST = 'smtp.163.com'  
SERVER_EMAIL = '234327894-cold@163.com'  # 
EMAIL_HOST_USER = '234327894-cold@163.com'  # 
EMAIL_HOST_PASSWORD = '234327894123'  #

Django使用AdminEmailHandler为它收到的每个日志消息向站点管理员发送电子邮件。 除了Django,我们还可以使用Python的 logging.handlers.SMTPHandler(mailhost, fromaddr, toaddrs, subject, credentials=None, secure=None, timeout=1.0) 做同样的事。 例如,将以下代码放在views.py(更改您的帐户),它将通过电子邮件报告未处理的异常并导致内部服务器错误(HTTP状态代码500)。在

^{pr2}$

比较AdminEmailHandler和{},我主张尽可能使用{}。 首先,AdminEmailHandler是Django特有的,虽然您可以在任何Python程序中使用SMTPHandler,但是您应该注意的一点是,在客户端软件中使用SMTPHandler时,某些防病毒软件可能会将该软件视为间谍软件,因此您应该在软件即将发送电子邮件时通知用户。 第二,我发现AdminEmailHandler发送的电子邮件包含大量信息,SMTPHandler只发送Python异常信息,这使得调试变得更加清晰了!在

第三,如果您在Django中的settings.py中配置电子邮件,那么即使您在电子邮件确认中出错,SMTPHandler也不会抛出异常,而{}则始终会抛出异常,指出使用中的错误。在

引自http://redstoneleo.blogspot.com/2016/12/email-reporting-exceptions-and-errors_30.html

相关问题 更多 >

    热门问题