I tried adding a TimezoneMiddleware
very similar to the one documented in the Django docs
This works great in the web side, but it broke all of my views that were using the Django TestClient. It seems that something about this middleware existing in tests causes the response
object to be None
in other middlewares, which triggers errors like this one:
======================================================================
ERROR: test_landing_page (apps.web.tests.test_basic_views.TestBasicViews.test_landing_page)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/czue/src/personal/wedding_plan/apps/web/tests/test_basic_views.py", line 7, in test_landing_page
self._assert_200(reverse("web:home"))
File "/home/czue/src/personal/wedding_plan/apps/web/tests/test_basic_views.py", line 22, in _assert_200
response = self.client.get(url)
^^^^^^^^^^^^^^^^^^^^
File "/home/czue/.virtualenvs/wedding_plan/lib/python3.11/site-packages/django/test/client.py", line 927, in get
response = super().get(path, data=data, secure=secure, headers=headers, **extra)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/czue/.virtualenvs/wedding_plan/lib/python3.11/site-packages/django/test/client.py", line 457, in get
return self.generic(
^^^^^^^^^^^^^
File "/home/czue/.virtualenvs/wedding_plan/lib/python3.11/site-packages/django/test/client.py", line 609, in generic
return self.request(**r)
^^^^^^^^^^^^^^^^^
File "/home/czue/.virtualenvs/wedding_plan/lib/python3.11/site-packages/django/test/client.py", line 891, in request
self.check_exception(response)
File "/home/czue/.virtualenvs/wedding_plan/lib/python3.11/site-packages/django/test/client.py", line 738, in check_exception
raise exc_value
File "/home/czue/.virtualenvs/wedding_plan/lib/python3.11/site-packages/django/core/handlers/exception.py", line 55, in inner
response = get_response(request)
^^^^^^^^^^^^^^^^^^^^^
File "/home/czue/.virtualenvs/wedding_plan/lib/python3.11/site-packages/django/utils/deprecation.py", line 136, in __call__
response = self.process_response(request, response)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/czue/.virtualenvs/wedding_plan/lib/python3.11/site-packages/django/middleware/common.py", line 107, in process_response
if response.status_code == 404 and self.should_redirect_with_slash(request):
^^^^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'status_code'
Switching it from the new-style middleware to the old one (using MiddlewareMixin
and process_request
, etc.) fixes the tests. But it seems like this path is meant to be deprecated.
Any advice on this?