简体   繁体   English

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

[英]How to use 'async def' in django views?

#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()),
]

I get this:我明白了:

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

##########

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

In django 3.0, they started adding django support but this does not mean that we can use async views or middlewares.在 django 3.0 中,他们开始添加 django 支持,但这并不意味着我们可以使用异步视图或中间件。

Read more about it here: https://docs.djangoproject.com/en/3.0/topics/async/在此处阅读更多相关信息: https : //docs.djangoproject.com/en/3.0/topics/async/

Django 3.0 was the first step towards making it fully async-capable, but it will be a long journey. Django 3.0 是使其完全具备异步能力的第一步,但这将是一段漫长的旅程。 Async views are not yet supported , but they are expected later in the 3.x series.尚不支持异步视图,但预计会在 3.x 系列的后期出现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM