简体   繁体   English

uWSGI + Django:如何将Django的过时警告视为错误?

[英]uWSGI + Django: How can I get Django's deprecation warnings to be treated as errors?

My goal is to treat Django's deprecation warnings as errors. 我的目标是将Django的过时警告视为错误。 I've been experimenting with a copy of Django 1.9 and haven't been able to get it work like (I think) it should with uWSGI. 我一直在尝试使用Django 1.9副本,但无法使其与uWSGI兼容(我认为)。 This is what I've done: 这是我所做的:

In my settings.py file, I changed django. template .context_processors.debug 在我的settings.py文件中,我更改了django. template .context_processors.debug django. template .context_processors.debug to django. core .context_processors.debug django. template .context_processors.debugdjango. core .context_processors.debug django. core .context_processors.debug in order to cause RemovedInDjango110Warning to be raised. django. core .context_processors.debug ,以引起RemovedInDjango110Warning的出现。 I then ran runserver and accessed the site. 然后,我运行runserver并访问了该站点。 The warning showed up in the runserver output but, as expected, it wasn't treated like an error. 该警告显示在runserver输出中,但是,正如预期的那样,它没有被视为错误。

Next, I ran export PYTHONWARNINGS=error . 接下来,我运行export PYTHONWARNINGS=error After that, I ran runserver and accessed the site. 之后,我运行了runserver并访问了该站点。 This time, the warning was treated like an error, which is what I want. 这次,警告被视为错误,这正是我想要的。

In an attempt to replicate this behavior with uWSGI, I added this to my uWSGI conf file: 为了尝试使用uWSGI复制此行为,我将其添加到了uWSGI conf文件中:

env = PYTHONWARNINGS=error

I then accessed the site. 然后,我访问了该站点。 The warning showed up in the uWSGI log, but it wasn't treated like an error. 该警告显示在uWSGI日志中,但是并未被视为错误。 To confirm that the PYTHONWARNINGS environment variable was being set, I added this to my wsgi.py file: 为了确认是否设置了PYTHONWARNINGS环境变量,我将其添加到了wsgi.py文件中:

print('PYTHONWARNINGS: %s' % os.environ.get('PYTHONWARNINGS', '[not set]'))

This caused PYTHONWARNINGS: error to be output in the uWSGI log, so I'm not sure what the problem is. 这导致PYTHONWARNINGS: error在uWSGI日志中输出,所以我不确定是什么问题。 Any ideas? 有任何想法吗?

It's not uWSGI configuration solution, but you can add the following snippet to the top of your project's urls.py : 它不是uWSGI配置解决方案,但您可以将以下代码段添加到项目的urls.py顶部:

import warnings
warnings.simplefilter('error', DeprecationWarning)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM