我目前正在运行 Vue + DRF 应用程序。当表单中的某些内容不符合标准并发送到 API 时,它会按预期返回 400 Bad 请求。检查“网络”选项卡中的响应,您会收到一条明确的验证错误消息,例如{"zip_code":["This field must not be blank"]}
。
问题是,我如何提取它并将其显示在我的 Vue 应用程序中?尝试捕获错误只会返回Uncaught (in promise) Error: Request failed with status code 400 at eval
而不是实际的错误消息(“此字段不能为空”)。
代码修补看起来像这样:
axios.patch(`users/${this.$route.params.id}/`, this.user)
.then(...)
.catch(error => {...}) // get error message and display it frontend to user
Run Code Online (Sandbox Code Playgroud) 我最近开始使用 mypy,并遇到了一些奇怪的问题,我似乎一生都无法弄清楚。
我正在使用 mypy 0.950、django-stubs 1.11.0、django 4.0.5 和 python 3.10.2。
通过命令行运行 mypy 将返回以下内容:
project/suppliers/models.py:6: error: Name "Optional" is not defined
project/suppliers/models.py:6: note: Did you forget to import it from "typing"? (Suggestion: "from typing import Optional")
project/users/models.py:6: error: Name "Optional" is not defined
project/users/models.py:6: note: Did you forget to import it from "typing"? (Suggestion: "from typing import Optional")
project/products/models.py:6: error: Name "Optional" is not defined
project/products/models.py:6: note: Did you forget to import it from "typing"? (Suggestion: "from typing import Optional")(Suggestion: "from …
Run Code Online (Sandbox Code Playgroud)