django admin页面日志中包含大量错误的处理方法

访问 django admin 页面的时候,日志中存在大量的错误信息如下,看上去都是找不到变量定义造成的:

DEBUG:django.template:Exception while resolving variable 'is_popup' in template 'admin/login.html'.
Traceback (most recent call last):
  File "/home/leandro/.envs/********/lib/python3.4/site-packages/django/template/base.py", line 879, in _resolve_lookup
    current = current[bit]
  File "/home/leandro/.envs/*******/lib/python3.4/site-packages/django/template/context.py", line 77, in __getitem__
    raise KeyError(key)
KeyError: 'is_popup'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/leandro/.envs/********/lib/python3.4/site-packages/django/template/base.py", line 885, in _resolve_lookup
    if isinstance(current, BaseContext) and getattr(type(current), bit):
AttributeError: type object 'RequestContext' has no attribute 'is_popup'

During handling of the above exception, another exception occurred:

此问题的原因在于 django 模板使用变量的时候,某些情况下会先在父页面中进行变量定义,然后子页面中根据是否存在变量定义做不同的页面渲染。 如果遇到未定义错误则捕捉后使用 Debug 级别进行日志记录,对此官方文档中有描述

比较简单的解决方案是将 template 的日志级别修改为 Info:

'loggers': {
  # ... other loggers
  'django.template': {
     'handlers': ['file_handler'],
     'level': 'INFO',
     'propagate': True,
  },
}

以上解决方案参考 StackOverflow