#32077: HTTP view decorators (require_http_methods) not working with async views
-------------------------------------+-------------------------------------
Reporter: Hendrik | Owner: nobody
Frentrup |
Type: New | Status: new
feature |
Component: HTTP | Version: 3.1
handling | Keywords: async views
Severity: Normal | decorators
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
HTTP view decorators don't work with async views, though I don't see why
they shouldn't given they don't use the ORM (unlike the login_required
decorator for example).
The following throws a `ValueError`
{{{
@require_http_methods(['GET'])
async def blog_info(request, blog_id):
blog = await sync_to_async(Blog.objects.get)(id=blog_id)
return HttpResponse(f"got spot #id: {blog_id} - name: {blog.name}")
}}}
Here is the error:
{{{
ValueError: The view myapp.views.blog_info didn't return an HttpResponse
object. It returned an unawaited coroutine instead. You may need to add an
'await' into your view.
}}}
From what I can see this is due to the decorator calling the async
function after checking the request headers and returning but returning a
non-async function (`return inner`) in `django.views.decorators.http`
How to handle middleware with async seems to be a point of contention
still, but these decorators are hardly middleware, so I think it would be
worth fixing. From some initial testing, it doesn't seem to be a difficult
fix either.
--
Ticket URL: <https://code.djangoproject.com/ticket/32077>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/058.42ebeedea320057854327c8b745f65a3%40djangoproject.com.