Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django:登录manage.py会导致错误的异常和回溯_Python_Django_Logging - Fatal编程技术网

Python Django:登录manage.py会导致错误的异常和回溯

Python Django:登录manage.py会导致错误的异常和回溯,python,django,logging,Python,Django,Logging,在manage.py文件中,我想在每次管理命令失败时向管理员发送一封电子邮件 下面的代码 manage.py import logging logger = logging.getLogger('management_commands') try: execute_from_command_line(sys.argv) except Exception as e: logger.error('Admin Command Error: %s', ' '.join(sys.argv

manage.py
文件中,我想在每次管理命令失败时向管理员发送一封电子邮件

下面的代码

manage.py

import logging
logger = logging.getLogger('management_commands')

try:
    execute_from_command_line(sys.argv)
except Exception as e:
    logger.error('Admin Command Error: %s', ' '.join(sys.argv),
                 exc_info=sys.exc_info())
    raise e
提高

"The translation infrastructure cannot be initialized before the "

django.core.exceptions.AppRegistryNotReady: The translation infrastructure cannot be initialized before the apps registry is ready. Check that you don't make non-lazy gettext calls at import time.`
当实际错误实际上是ImportError时:没有名为django_inlinecss的模块

我对记录器的设置是

LOGGING = {
    ...
    'handlers': {
        ...
        'mail_admins': {
            'class': 'django.utils.log.AdminEmailHandler',
            'level': 'ERROR',
            'include_html': True
        }
    },
    'loggers': {
        ...
        'management_commands': {
            'handlers': ['mail_admins'],
            'level': 'ERROR',
            'propagate': True
        }
    }
}
追溯的第一部分是

File "/usr/lib/python2.7/logging/__init__.py", line 1279, in _log
    self.handle(record)
File "/usr/lib/python2.7/logging/__init__.py", line 1289, in handle
    self.callHandlers(record)
File "/usr/lib/python2.7/logging/__init__.py", line 1329, in callHandlers
    hdlr.handle(record)
File "/usr/lib/python2.7/logging/__init__.py", line 757, in handle
    self.emit(record)
File "/usr/local/lib/python2.7/dist-packages/django/utils/log.py", line 128, in emit
    html_message = reporter.get_traceback_html() if self.include_html else None
File "/usr/local/lib/python2.7/dist-packages/django/views/debug.py", line 384, in get_traceback_html
    return t.render(c)
File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 210, in render
    return self._render(context)
这就是为什么我最终认为可能存在与日志相关的问题

python日志库使Django提升
“在”

之前无法初始化翻译基础结构的任何原因您已将“mail\u admins”日志程序配置为包含html,这会触发调试视图中某些模板呈现的执行,这本身就要求django的翻译系统被初始化

这里最简单的解决方案是配置另一个不带“include_html”标志的处理程序,并将此处理程序用于记录器,即(警告:完全未测试):


此版本的
manage.py
如果您尚未安装
installed\u APPS
中列出的应用程序,也会发生此错误


您可以删除
manage.py
中的except子句,这样就可以在不进行复杂渲染的情况下显示异常,从而避免了令人困惑的错误。

谢谢。事实上,问题是因为
include\u html:True
。它可以正常工作,无需安装。
# logging config

LOGGING = {
    ...
    'handlers': {
        ...
        'mail_admins': {
            'class': 'django.utils.log.AdminEmailHandler',
            'level': 'ERROR',
            'include_html': True
        },
        'command_mail_admins': {
            'class': 'django.utils.log.AdminEmailHandler',
            'level': 'ERROR',
            'include_html': False
        },
    },
    'loggers': {
        ...
        'management_commands': {
            'handlers': ['command_mail_admins'],
            'level': 'ERROR',
            'propagate': True
        }
    }
}