【问题标题】:How to return a file in django如何在 django 中返回文件
【发布时间】:2021-09-11 17:28:01
【问题描述】:

我有一个 django 应用程序,我想通过它提供一个 index.html 文件。这个 html 文件不在默认的 django 模板中,而是我在 react 构建文件夹中有 index.html。我不想渲染模板,而是想发送它。 render 方法不起作用,我不知道是否有任何 django 方法可以为文件提供服务。我想要完成的可以在 expressjs 中使用res.sendFile() 来完成。

def home(request):
    return FileResponse(request, 'home/home.html')    

FileResponse 方法在这里不起作用。

【问题讨论】:

标签: python django express templates


【解决方案1】:

也许你可以试试这个,你可以专门将它用于 CSV,但如果你想在 Djagngo 的指南中查看 PDF 的示例Link

【讨论】:

  • 感谢@John Bull。但是,我想提供的不是 csv 文件。我想要在 expressjs 中找到与 res.sendFile() 等效的 django
  • 试试:the_file = open("somefile.txt", "wb", buffering=False) response = json(the_file)
猜你喜欢
  • 2017-08-06
  • 1970-01-01
  • 2011-02-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-02
  • 2021-10-19
  • 1970-01-01
相关资源
最近更新 更多