【问题标题】:How to use 'async def' in django views?如何在 django 视图中使用'async def'?
【发布时间】:2020-02-29 02:23:29
【问题描述】:
#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


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      Django 3.0 是使其完全支持异步的第一步,但这将是一段漫长的旅程。异步视图是 not yet supported,但预计在 3.x 系列的后期。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-09-03
        • 1970-01-01
        • 2017-03-27
        • 1970-01-01
        • 2023-01-17
        • 2015-08-15
        相关资源
        最近更新 更多