我正在使用wkhtmltopdf,PDFTemplateView生成pdf文件,并且想在生成完成后呈现到另一个视图:
URL:
url(r'^admin/mymodel/(?P<pdf_id>\d+)/generate/(?P<pk>\d+)/?$',
my_views.MyModelPdfView.as_view(),
name="pdf-view"),
url(r'^admin/mymodel/(?P<pdf_id>\d+)/', my_views.pdf_create_view,
name="pdf-create"),
在自定义管理员视图中,我有一个自定义按钮,可生成pdf:
@staff_member_required
def pdf_create_view(request, *args, **kwargs):
if request.method == 'POST':
/**** Treating data then calling the pdf view***/
return redirect('pdf-view', pdf_id=kwargs.get('pdf_id'),
pk=commande.id)
在我的 PDF视图中
class MyModelPdfView(PDFTemplateView):
model = MyModel
template_name = 'admin/my_template.html'
cmd_options = {
'page-size': 'A4',
'margin-top': 0,
'margin-left': 0,
'margin-bottom': 0,
'margin-right': 0,
}
def get_context_data(self, **kwargs):
context = super(MyModelPdfView, self).get_context_data(**kwargs)
context['object'] = MyModel.objects.get(id=self.kwargs.get('pk'))
return context