使用基于Django类的视图生成PDF

时间:2016-06-21 03:42:26

标签: django pdf django-views

我目前使用基于类的视图实现了库贷款模板。实施可能是我们以前见过的:

urls.py

#PK is the member ID  
url(r'^library/generate_loan_slip/(?P<pk>(\d+))/(?P<loan>(\d+))/'
        ,LoanSlipDetailView.as_view()
        , name='library_app_generate_loan_slip'),

views.py

class LoanSlipDetailView(DetailView):
    model = LibraryMember
    loan = None
    template_name = 'loan-slip.html'

    def get_context_data(self, **kwargs):
        context['loan'] = self.loan
        context['member'] = member
        return context

    def get_object(self):
        member = self.model.objects.get(pk=self.kwargs['pk'])
        self.loan = LibraryLoan.objects.get(pk=self.kwargs['loan'], for_member=member)
        return member

在HTML loan-slip.html中,html包含{{}}中的所有必需变量,并使用{% if %} ... {% else %} ... {% endif %}{% for ... %} ... {% endfor %}进行控制。

现在,我希望响应生成PDF视图。到目前为止,我已经检查了reportlab并且他们为基于函数的视图实现了它。但是,是否有基于类的视图的实现?

1 个答案:

答案 0 :(得分:0)

您可以覆盖DetailView的get方法并在那里编写reportlab逻辑。