![](/img/trans.png)
[英]How can I loop through json file and return it as HttpRespones in Django?
[英]How can I return a redirect and file in Django?
我正在處理一個表單,提交時可以選擇導出 PDF。 這很好,但我也希望能夠返回一個新的 URL 以便用戶被重定向到以及下載文件。 將render()
或redirect()
與FileResponse()
結合起來的東西。
為此,您將需要兩個視圖。 一個用於重定向,另一個用於提供要下載的文件。 這是一個簡化的實現:
def myform(request):
form = MyForm(request.POST)
if form.is_valid():
return redirect("/download/file.pdf")
def download(request):
file = open("file.pdf", "rb")
return FileResponse(file, as_attachment=True)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.