djnago忍者|尝试发送文件|找不到< class' django.http.response.httpresponse>>,请参见coniform

发布于 2025-01-21 05:36:36 字数 761 浏览 0 评论 0 原文

好人,

我正在尝试实现文件下载功能。而且代码非常简单:

@api.get("/summary/", response=HttpResponse)
def report_summary(
    request: NinjaRequest, start_date: str, end_date: str
) -> HttpResponse:
    ...
    response = HttpResponse(
        output, # output is the file
        content_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
    )
    response["Content-Disposition"] = f"attachment; filename={fname}" # fname is the name of the file
    return response

但是在启动过程中,我说的是一个错误:

RuntimeError: no validator found for <class 'django.http.response.HttpResponse'>, see `arbitrary_types_allowed` in Config

我不想设置 nuterary_types_lowled

现在如何解决此问题?

good people,

I'm trying to implement a file download functionality. And the code is pretty straightforward:

@api.get("/summary/", response=HttpResponse)
def report_summary(
    request: NinjaRequest, start_date: str, end_date: str
) -> HttpResponse:
    ...
    response = HttpResponse(
        output, # output is the file
        content_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
    )
    response["Content-Disposition"] = f"attachment; filename={fname}" # fname is the name of the file
    return response

But it gives me an error saying during bootup:

RuntimeError: no validator found for <class 'django.http.response.HttpResponse'>, see `arbitrary_types_allowed` in Config

I don't want to set arbitrary_types_allowed.

Now how can I resolve this issue?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我纯我任性 2025-01-28 05:36:36

根据:

删除此部分

, response=HttpResponse)

,实际上起作用

According to: https://github.com/vitalik/django-ninja/issues/424#issuecomment-1099930539

remove this part

, response=HttpResponse)

And it actually worked

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文