Django 2.2 breaks previously working views/urls

前端 未结 4 931
花落未央
花落未央 2021-01-18 23:52

Decided to take out Django 2.2 for a spin (project is currently running 2.1.8) and now I can\'t even get the server to start. I have been maintaining this project for nearly

相关标签:
4条回答
  • 2021-01-19 00:17

    It seems the custom error handlers are the cause of it.

    In Django 2.1 I had a custom handler for a 500 Error error like this:

    def error_500_view(request, exception):
        return render(request,'500.html')
    

    But in Django 2.2 it seems that the 500 Error handler only takes 1 argument, so I changed to:

    def error_500_view(request):
        return render(request,'500.html')
    

    And everything is working normal again.

    So make sure your 404 Error handler is something like:

    def notfound(request, exception):
        return render(request,'400.html')
    
    0 讨论(0)
  • 2021-01-19 00:18

    This is a two step problem:

    First, something changed with how Django is resolving paths to views. Whereas handler404 = 'views.error_404' worked on 2.1.8, the more explicit path handler404 = 'apps.dashboard.views.error_404' is required on 2.2.

    Second, I made a mistake with how I wrote my custom handler404 and a new system check caught it before the server starts. handler404 is supposed to be setup to take two arguments request, exception, whereas I had it simply setup to accept request. It may have been failing silently.

    I ran into a third error LookupError: No installed app with label 'admin' after I initially fixed the path, but after uninstalling and reinstalling 2.2, I have not been able to replicate it.

    Thanks to @Alasdair we've got a ticket open and it looks like this will be painless to fix in 2.2.1.

    I'm simply writing this answer to summarize the complete issue, as both of the fantastic answers previously given only cover one part. Thanks guys!

    0 讨论(0)
  • 2021-01-19 00:31

    The traceback shows that the new _check_custom_error_handlers system check is raising an error. This suggests that you have an invalid custom error handler in your urls.py, e.g.

    handler404 = 'views.notfound'
    

    From image of your project layout, it looks as if that should be something like:

    handler404 = 'apps.dashboard.views.notfound'
    

    The custom error handler check was added in Django 2.2, so you are now notified about the problem when you start runserver. In the past, Django would have tried to load the custom handler later, and it looks like you didn't notice that it failed to load.

    In Django 2.2.1, there is a new system check, so you will get a more useful error message when the custom error handler cannot be imported.

    0 讨论(0)
  • 2021-01-19 00:35

    Another thing is handler(request, *args), but essentially it's the same as what is suggested above.

    0 讨论(0)
提交回复
热议问题