Django:追踪DeprecationWarning的原因

时间:2021-09-28 20:58:49

I've upgraded to Django 1.4 and now when I run my development server I get the following warning:

我已升级到Django 1.4,现在当我运行我的开发服务器时,我收到以下警告:

/home/flc/venvs/myprj/lib/python2.6/site-packages/django/views/generic/simple.py:8:

DeprecationWarning: Function-based generic views have been deprecated; use class-based views instead. DeprecationWarning

弃用警告:已弃用基于函数的通用视图;而是使用基于类的视图。 DeprecationWarning

I have tracked down most of the causes of this and fixed them by making the following changes:

我已经找到了大部分原因,并通过进行以下更改来修复它们:

django.views.generic.simple.direct_to_template => django.views.generic.base.TemplateView django.views.generic.simple.redirect_to => django.views.generic.base.RedirectView

django.views.generic.simple.direct_to_template => django.views.generic.base.TemplateView django.views.generic.simple.redirect_to => django.views.generic.base.RedirectView

etc

等等

However, I'm still getting the warning and can't figure out what I've missed. How do I get the actual module and line in my code that is causing the DeprecationWarning?

但是,我仍然收到警告,无法弄清楚我错过了什么。如何获取导致DeprecationWarning的代码中的实际模块和行?

2 个解决方案

#1


23  

You can use the warnings module to raise an error for DeprecationWarning.

您可以使用警告模块为DeprecationWarning引发错误。

Temporarily add the following snippet to the top of your project's urls.py:

暂时将以下代码段添加到项目的urls.py顶部:

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

The DeprecationWarning will now raise an error, so if debug=True you'll get the familiar yellow Django error page with the full traceback.

DeprecationWarning现在会引发错误,因此如果debug = True,您将获得熟悉的黄色Django错误页面以及完整的回溯。

Once you've tracked down the source of the deprecation warnings, remember to remove the snippet! Note that it may be a third party app that is causing the deprecation warnings, not your own code.

一旦您追踪了弃用警告的来源,请记住删除该片段!请注意,它可能是导致弃用警告的第三方应用程序,而不是您自己的代码。

If you're new to the warnings module, you might find the page on Python module of the week to be an easier introduction than the Python docs.

如果您是警告模块的新手,您可能会发现本周Python模块上的页面比Python文档更容易介绍。

#2


10  

You can also do this on the command line so you don't need to modify your code. For example:

您也可以在命令行上执行此操作,这样您就不需要修改代码。例如:

python -We manage.py runserver --traceback

The official doc is here. You can use abbreviations and the e in -We stands for convert warnings to error.

官方文件在这里。您可以使用缩写和e in -We代表将警告转换为错误。

#1


23  

You can use the warnings module to raise an error for DeprecationWarning.

您可以使用警告模块为DeprecationWarning引发错误。

Temporarily add the following snippet to the top of your project's urls.py:

暂时将以下代码段添加到项目的urls.py顶部:

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

The DeprecationWarning will now raise an error, so if debug=True you'll get the familiar yellow Django error page with the full traceback.

DeprecationWarning现在会引发错误,因此如果debug = True,您将获得熟悉的黄色Django错误页面以及完整的回溯。

Once you've tracked down the source of the deprecation warnings, remember to remove the snippet! Note that it may be a third party app that is causing the deprecation warnings, not your own code.

一旦您追踪了弃用警告的来源,请记住删除该片段!请注意,它可能是导致弃用警告的第三方应用程序,而不是您自己的代码。

If you're new to the warnings module, you might find the page on Python module of the week to be an easier introduction than the Python docs.

如果您是警告模块的新手,您可能会发现本周Python模块上的页面比Python文档更容易介绍。

#2


10  

You can also do this on the command line so you don't need to modify your code. For example:

您也可以在命令行上执行此操作,这样您就不需要修改代码。例如:

python -We manage.py runserver --traceback

The official doc is here. You can use abbreviations and the e in -We stands for convert warnings to error.

官方文件在这里。您可以使用缩写和e in -We代表将警告转换为错误。