[Django]-Tests for view names failing since upgrading to Django 4.0

3👍

You use .as_view(), it thus does not return a class object, but a function that will handle requests by creating a new HomeView and trigger the corresponding method based on the HTTP method.

You can check if it is a method of the HomeView by checking the .view_class attribute. Furthermore it is probably better to check if the two classes are the same, not the names of these classes:

#                                            ↓ no .__name__  ↓
self.assertEqual(resolve('/').func.view_class, views.HomeView)

I did some research why comparing the __name__s no longer works. Apparently in they used the update_wrapper(…) function which sets the __name__, __qualname__, etc. of one object (in this case the class) to the function. Indeed, in the source code [GitHub], we can see:

@classonlymethod
def as_view(cls, **initkwargs):
    # …
    def view(request, *args, **kwargs):
        # …
    # …

    # take name and docstring from class
    update_wrapper(view, cls, updated=())
    # …

this has been changed in where they also explained in a comment why they do no longer do that [GitHub]:

@classonlymethod
def as_view(cls, **initkwargs):
    # …
    # __name__ and __qualname__ are intentionally left unchanged as
    # view_class should be used to robustly determine the name of the view
    # instead.
    # …

Leave a comment