python - 如何在 django 视图中使用'async def'?
问题描述
#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'
解决方案
在 django 3.0 中,他们开始添加 django 支持,但这并不意味着我们可以使用异步视图或中间件。
在此处阅读更多信息: https ://docs.djangoproject.com/en/3.0/topics/async/
推荐阅读
- typescript - 'string | 类型的参数 undefined' 不能分配给“untitled”类型的参数 | 空 | 不明确的'
- python - 如何避免此列表中的舍入错误?
- javascript - 如何使用 thunk 根据 redux 状态有条件地显示小吃店?
- python-3.x - Pytest用户输入模拟
- apache-kafka - Kafka重试同一主题
- javascript - 带键盘的视频播放器控制
- java - 解析文本的 Java 方法仅针对正数返回 StringIndexOutOfBoundsException
- kubernetes - GKE 如何创建保留的公共或私有 gcp 地址以用于 LoadBalancer 服务?
- ruby - 比较 2 个 DateTime 实例,它给了我错误的结果
- java - intellij IDEA 2018.1.6 无法识别 import com.mongodb.MongoClient