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

iMa*_*ath 5 python django python-3.x

我可以使用以下代码发送邮件

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
>>> 
Run Code Online (Sandbox Code Playgroud)

根据文档

当 DEBUG 为 False 时,每当您的代码引发未处理的异常并导致内部服务器错误(HTTP 状态代码 500)时,Django 都会向 ADMINS 设置中列出的用户发送电子邮件。这为管理员提供了任何错误的即时通知。ADMINS 将获得错误描述、完整的 Python 回溯以及有关导致错误的 HTTP 请求的详细信息。

但就我而言,Django 不会通过电子邮件报告内部服务器错误(HTTP 状态代码 500) 在此处输入图片说明

有什么问题?请帮助解决问题

设置.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/'
Run Code Online (Sandbox Code Playgroud)

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)])
...
Run Code Online (Sandbox Code Playgroud)

Ant*_*des 2

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

其次,最好尝试在控制台中使用与 中设置的电子邮件地址完全相同的电子邮件地址ADMINS,以确保万无一失。

最后,发件人地址也可能很重要。默认值为“root@localhost”,虽然“root”可以,但“localhost”则不行,并且某些邮件服务器可能会拒绝电子邮件。通过设置 Django 设置来指定另一个发件人电子邮件地址SERVER_EMAIL