#35133 closed New feature (duplicate)

Make @method_decorator to work with async methods.

Reported by: unnamedtrue Owned by: nobody
Component: Utilities Version: 5.0
Severity: Normal Keywords: never_cache, decorator, async, classnased view, dispatch, coroutine
Cc: Ben Lomax Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

If I use never_cache decorator for my classbased View I get this error:
"'coroutine' object has no attribute 'has_header'"
Code is:

@method_decorator(never_cache, name="dispatch")
class CheckAsync(View):
    async def get(self, request, *args, **kwargs):
        # Perform io-blocking view logic using await, sleep for example.
        await asyncio.sleep(1)
        return HttpResponse("Hello async world!")

Debug info:

Request Method: GET
Request URL: http://127.0.0.1:8000/api/asynccheck/

Django Version: 5.0.1
Python Version: 3.12.1
Installed Applications:
['core.apps.AdminConfig',
 'django.contrib.sites',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'api.apps.ApiAppConfig',
]
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'django.contrib.sites.middleware.CurrentSiteMiddleware',
 'django.middleware.http.ConditionalGetMiddleware',
 'django.middleware.gzip.GZipMiddleware']

Traceback (most recent call last):
  File "/Users/me/p/lib/python3.12/site-packages/django/core/handlers/exception.py", line 55, in inner
    response = get_response(request)
               ^^^^^^^^^^^^^^^^^^^^^
  File "/Users/me/p/lib/python3.12/site-packages/django/core/handlers/base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/concurrent/futures/_base.py", line 449, in result
    return self.__get_result()
           ^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/concurrent/futures/_base.py", line 401, in __get_result
    raise self._exception
    ^^^^^^^^^^^^^^^^^^^^^
  File "/Users/me/p/lib/python3.12/site-packages/django/views/generic/base.py", line 104, in view
    return self.dispatch(request, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/me/p/lib/python3.12/site-packages/django/utils/decorators.py", line 48, in _wrapper
    return bound_method(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/me/p/lib/python3.12/site-packages/django/views/decorators/cache.py", line 81, in _view_wrapper
    add_never_cache_headers(response)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/me/p/lib/python3.12/site-packages/django/utils/cache.py", line 292, in add_never_cache_headers
    patch_response_headers(response, cache_timeout=-1)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/me/p/lib/python3.12/site-packages/django/utils/cache.py", line 283, in patch_response_headers
    if not response.has_header("Expires"):
           ^^^^^^^^^^^^^^^^^^^

Exception Type: AttributeError at /api/asynccheck/
Exception Value: 'coroutine' object has no attribute 'has_header'

Change History (3)

comment:1 by Mariusz Felisiak, 11 months ago

Cc: Ben Lomax added
Component: Core (Cache system)Utilities
Summary: never_cache decorator problem with async get function inside classbased ViewMake @method_decorator to work with async methods.
Type: BugNew feature

As far as I'm aware, @method_decorator doesn't support decorating async methods. Tentatively accepted as a new feature.

comment:2 by Mariusz Felisiak, 11 months ago

Triage Stage: UnreviewedAccepted

comment:3 by Mariusz Felisiak, 11 months ago

Resolution: duplicate
Status: newclosed
Triage Stage: AcceptedUnreviewed

Duplicate of #35083 (thanks David!).

Note: See TracTickets for help on using tickets.
Back to Top