22

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

/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

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

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?

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
FunLovinCoder
  • 7,597
  • 11
  • 46
  • 57

2 Answers2

24

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

Temporarily add the following snippet to the top of your project's 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.

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.

Alasdair
  • 298,606
  • 55
  • 578
  • 516
  • Looks like it was the registration package that was causing the DepricationWarning. Would have take me much longer to track down without your snippet. Many thanks. – FunLovinCoder Sep 04 '12 at 16:35
  • This smells like it should be a setting- DEBUG_WARNINGS or similar (as much as we all loathe the idea of more settings) – s29 Feb 05 '13 at 20:58
  • @s29 Why add yet another setting when the answer above only requires two lines of code to implement? – Alasdair Feb 06 '13 at 14:07
  • 1
    "Batteries included", that's why. I shouldn't have to google/stackoverflow something like this. – s29 Feb 07 '13 at 23:41
  • 1
    It would be useful if the snippet above was included in the Django docs, but that doesn't mean it justifies a setting. What if you want to treat `PendingDeprecationWarning` and `DeprecationWarning` differently? Once you've learned about `warnings.simplefilter`, it would be trivial to change. If the functionality is hidden behind a setting, then you have to go back to the Django docs. To me, "batteries included" means you shouldn't have to, say, implement your own function to decode form-encoded data. Controlling warnings isn't that complicated though, it's one line of code after the import. – Alasdair Feb 08 '13 at 01:18
11

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.

user193130
  • 8,009
  • 4
  • 36
  • 64