如何在Django视图中使用'async def'?

问题描述 投票:0回答:2
#views.py

async def test(request: ASGIRequest):
    return HttpResponse(b'hello')

class Test(View):
    async def get(self, request: ASGIRequest):
        print(type(request))
        print(dir(self))
        return HttpResponse(b'hello')
#urls.py

urlpatterns = [
    path('admin/', admin.site.urls),
    path(r'testfunc/', test),
    path(r'testclass/', Test.as_view()),
]

我明白了:

AttributeError at /testclass/
'coroutine' object has no attribute 'get'

##########

AttributeError at /testfunc/
'coroutine' object has no attribute 'get'
python django asynchronous asgi
2个回答
0
投票

在django 3.0中,他们开始添加django支持,但这并不意味着我们可以使用异步视图或中间件。

在此处了解更多信息:https://docs.djangoproject.com/en/3.0/topics/async/


0
投票

Django 3.0是使其完全具备异步功能的第一步,但这将是一段漫长的旅程。异步视图为not yet supported,但预计将在3.x系列的更高版本中出现。

© www.soinside.com 2019 - 2024. All rights reserved.