Python django:如何下载模板
我在django应用程序中有一个表单,其中自动提取用户的数据,以及供用户在模板内部填写的可写字段 我构建了另一个html页面,其中包含pdf文档的形状,我正在尝试(未成功)获取用户在pdf中呈现的模板中输入的数据 我只能渲染pdf,但没有填充任何字段 老实说,我对这个项目的这一部分感到非常困惑,我无法弄清楚到底是什么导致了这个bug。我希望有人能帮助我 以下是用户编辑pdf的视图:Python django:如何下载模板,python,django,Python,Django,我在django应用程序中有一个表单,其中自动提取用户的数据,以及供用户在模板内部填写的可写字段 我构建了另一个html页面,其中包含pdf文档的形状,我正在尝试(未成功)获取用户在pdf中呈现的模板中输入的数据 我只能渲染pdf,但没有填充任何字段 老实说,我对这个项目的这一部分感到非常困惑,我无法弄清楚到底是什么导致了这个bug。我希望有人能帮助我 以下是用户编辑pdf的视图: def invoice_generator_assembly(request): challan_numb
def invoice_generator_assembly(request):
challan_number = ChallanNumber.objects.get(id=1)
works = Work.objects.all().order_by('code')
hsc = HSCNumber.objects.all()
company = MyCompany.objects.get(id=1)
context = {
'works' : works,
'hsc' : hsc,
'challan_number': challan_number,
'a' : company,
}
return render(request, 'invoice_assembly.html', context)
下面是呈现pdf的视图的外观:
def generate_pdf_assembly(request):
'''
Helper function to generate pdf in case of ajax request
'''
context = request.GET.copy()
context['works'] = json.loads(context['works'])
#context['amount_in_words'] = num2words.number_to_words(round(float(context.get('grand_total')), 2)) + ' only'
challan_number = context.get('challan_number')
date = context.get('date')
client_name = context.get('client_name')
client_address = context.get('client_address')
total_weight = context.get('total_weight')
total_amount = context.get('grand_total')
# If user enters the same challan number then the previous record for that paricular challan number
# is deleted and new record is overriden onto the old one
try:
report = Report.objects.create(
challan_number=challan_number,
date=date,
client_name = client_name,
client_address = client_address,
total_weight = total_weight,
total_amount=total_amount
)
report.save()
challan = ChallanNumber.objects.first()
challan.challan_number += 1
challan.save()
except:
QuantityRate.objects.filter(report__challan_number=challan_number).delete()
Report.objects.filter(challan_number=challan_number).delete()
report = Report.objects.create(
challan_number=challan_number,
client_name = client_name,
client_address = client_address,
date=date,
total_weight = total_weight,
total_amount=total_amount
)
report.save()
challan = ChallanNumber.objects.first()
challan.challan_number += 1
challan.save()
# Sometime user might delete a row dynamically and hence an empty dict is passed to server
# Hence we will check if amount is present in the dict else we delete that particular dic record
for index, work in enumerate(context.get('works')):
if work.get('amount'):
quant = QuantityRate.objects.create(
quantity=work.get('quantity'),
rate=work.get('rate'),
amount=work.get('amount')
)
quant.save()
quant.report.add(report)
else:
del context.get('works')[index]
context['date'] = datetime.datetime.strptime(date, '%Y-%m-%d').strftime('%d-%m-%Y')
context['dated'] = datetime.datetime.strptime(context.get('dated'), '%Y-%m-%d').strftime('%d-%m-%Y')
request.session['context'] = context
return redirect('get_pdf_assembly')
要获取pdf,请执行以下操作:
def get_pdf_assembly(request):
#context = request.GET.copy()
#request.session['context'] = context
pdf = render_to_pdf('pdf/invoice_generator_assembly.html', request.session['context'])
if pdf:
response = HttpResponse(pdf, content_type='application/pdf')
filename = "Assembly_Invoice_{}.pdf".format(request.session.get('context').get('challan_number'))
content = "inline; filename={}".format(filename)
content = "attachment; filename={}".format(filename)
response['Content-Disposition'] = content
return response
return HttpResponse("Not found")