Good afternoon Devs, a question:
I have a class based on Django-Bootstrap-Modal-Form/BSModalFormView that inherits from FormView, so far so good, in the form post I’m sent to the form_valid method, in this method I run a pdf rendering class with Pisa!
Everything works perfectly, my problem is that the PDF is generated, but quickly (in terms of seconds) the redirection to the get_success_url happens and my page is updated thus making it impossible to view the pdf.
Question: Is it possible to overwrite the form_valid so that it shows only the generated PDF and does not redirect to get_success_url???
class EmpresaReportView(BSModalFormView):
template_name = ‘report_form.html’
def form_valid(self, form):
Pdf.get(request, self)
response = super().form_valid(form)
return response
def get_success_url(self):
return reverse_lazy('list_empresas')
class Pdf(View):
def get(request, self):
params = {
‘title’: ‘teste de emissao de relatorio’,
‘today’: ‘variavel today’,
‘sales’: ‘variavel sales’,
‘request’: request,
}
return Render.render(‘empresas/reports/teste.html’, params, ‘myfile’)
class Render:
@staticmethod
def render(path: str, params: dict, filename: str):
template = get_template(path)
html = template.render(params)
response = io.BytesIO()
pdf = pisa.pisaDocument(
io.BytesIO(html.encode(“UTF-8”)), response)
if not pdf.err:
response = HttpResponse(
response.getvalue(), content_type=‘application/pdf’)
response[‘Content-Disposition’] = ‘inline; filename=%s.pdf’ % filename
return response
else:
return HttpResponse(‘Erro na renderização do PDF’, status=400)