[英]Generating Multiple Documents (Python TempFile Module)
I am currently trying to print financial reports with my web app - this needs to happen by the click of a button.我目前正在尝试使用我的 web 应用程序打印财务报告 - 这需要通过单击按钮来实现。 When I click the button, however, the app only prints 1 document (the first one) I don't understand what this could be as there are 2 different returns for each if statement that re-direct to 2 different.HTML pages但是,当我单击该按钮时,该应用程序仅打印 1 个文档(第一个)我不明白这可能是什么,因为每个 if 语句有 2 个不同的返回值,重定向到 2 个不同的页面。HTML 页
This is what I have tried at the moment:这是我目前尝试过的:
import tempfile
def printReports(request , reports_pk):
pkForm = get_object_or_404(SettingsClass , pk=reports_pk)
complexName = pkForm.Complex
if pkForm.Trial_balance_Year_To_Date == True:
### Printing Trial Balance PDF
response = HttpResponse(content_type= 'application/pdf')
response['Content-Disposition']= 'attachment; filename=TrialBalance' + \
str(datetime.now()) + '.pdf'
response['Content-Transfer-Encoding'] = 'binary'
content = {"x_AlltrbYTD":x_AlltrbYTD , 'xCreditTotal':xCreditTotal , 'xDebitTotal':xDebitTotal , 'complexName':complexName , 'openingBalances': openingBalances ,'printZero':printZero , 'printDesc':printDesc , 'printAcc':printAcc}
html_string=render_to_string('main/reports/trialBalanceYear.html' , content)
html=HTML(string=html_string)
result=html.write_pdf()
with tempfile.NamedTemporaryFile(delete=True) as output:
output.write(result)
output.flush()
output.seek(0)
response.write(output.read())
return response
if pkForm.Trial_balance_Monthly == True:
### Printing Trial Balance PDF
response = HttpResponse(content_type= 'application/pdf')
response['Content-Disposition']= 'attachment; filename=TrialBalanceMonthly' + \
str(datetime.now()) + '.pdf'
response['Content-Transfer-Encoding'] = 'binary'
content = {"xtrbMonth":xtrbMonth , 'xCreditTotalM':xCreditTotalM , 'xDebitTotalM':xDebitTotalM , 'complexName':complexName , 'printZeroM':printZeroM}
html_string=render_to_string('main/reports/trialBalanceMonthly.html' , content)
html=HTML(string=html_string)
result=html.write_pdf()
with tempfile.NamedTemporaryFile(delete=True) as output:
output.write(result)
output.flush()
output.seek(0)
response.write(output.read())
else:
printTrialBalanceMonth = False
The program only prints what the first if-statement shows and doesn't print the second.pdf document.该程序只打印第一个 if 语句显示的内容,不打印第二个 .pdf 文档。 Does anyone know what might be causing this?有谁知道这可能是什么原因造成的?
You are using return
in your first if
block.您在第一个if
块中使用return
。
Django generates a response and the code below return never runs (by the way it returns nothing, so you won't have a response even if pkForm.Trial_balance_Monthly == True
. Django 生成一个响应,下面的代码 return 永远不会运行(顺便说一句,它什么都不返回,所以即使pkForm.Trial_balance_Monthly == True
你也不会得到响应。
Modern IDE, such as Pycharm will show you a colored warning here, but if you are not using such IDE you can trace what's happening in your code using a debugger.现代 IDE,例如 Pycharm 会在此处显示彩色警告,但如果您不使用此类 IDE,则可以使用调试器跟踪代码中发生的情况。
To make such interaction as you've described (only one button and 2 reports) you need:要进行您所描述的交互(只有一个按钮和 2 个报告),您需要:
Make a temp zip file with two reports and create a response with only one zip file.创建一个包含两个报告的临时 zip 文件,并创建一个仅包含一个 zip 文件的响应。 Every OS has unzip feature, so your user will find this very intuitive.每个操作系统都有解压缩功能,因此您的用户会发现这非常直观。 Here is zipfile documentation这是压缩文件文档
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.