使用xhtml2pdf的Django GeneratePDF - 不允许的方法(GET)

时间:2018-06-11 08:52:28

标签: django xhtml2pdf

我会修改以下类GeneratePDF 来渲染动态生成的模板

在myproject / myapp / utils.py

from io import BytesIO
from django.http import HttpResponse
from django.template.loader import get_template
from xhtml2pdf import pisa

def render_to_pdf(template_src, context_dict={}):
    template = get_template(template_src)
    html  = template.render(context_dict)
    result = BytesIO()
    pdf = pisa.pisaDocument(BytesIO(html.encode("ISO-8859-1")), result)
    if not pdf.err:
        return HttpResponse(result.getvalue(), content_type='application/pdf')
    return None

views.py

class GeneratePDF(View):
def get(self, request, *args, **kwargs):
    template = get_template('invoice.html')
    context = {
        "invoice_id": 123,
        "customer_name": "John Cooper",
        "amount": 1399.99,
        "today": "Today",
    }
    html = template.render(context)
    pdf = render_to_pdf('invoice.html', context)
    if pdf:
        response = HttpResponse(pdf, content_type='application/pdf')
        filename = "Invoice_%s.pdf" %("123")
        content = "inline; filename='%s'" %(filename)
        download = request.GET.get("download")
        if download:
            content = "attachment; filename='%s'" %(filename)
        response['Content-Disposition'] = content
        return response
    return HttpResponse("Not found")

我使用以下内容生成我的模板:

class MyReport(ListView):
model = Reports
template_name = 'myreports.html' 
context_object_name = 'myreports'  
def get_context_data(self, *args,**kwargs):
    context = super(MyReport, self).get_context_data(*args,**kwargs)
    context['reports'] = Reports.objects.all().values('id','idsys','itemcode','ordercode').filter(id=self.kwargs['id'],idsys=self.kwargs['idsys'])
    return context

我尝试使用以下类修改生成PDF:

class GeneratePDF(View):
def get_context_data(self, *args,**kwargs):
    template = get_template('myreports.html')
    context = super(GeneratePDF, self).get_context_data(*args,**kwargs)
    context['reports'] = Reports.objects.all().values('id','idsys','itemcode','ordercode').filter(id=self.kwargs['id'],idsys=self.kwargs['idsys'])

    html = template.render(context)
    pdf = render_to_pdf('myreports.html', context)
    if pdf:
        response = HttpResponse(pdf, content_type='application/pdf')
        filename = "myreports_%s.pdf" %("1")
        content = "inline; filename='%s'" %(filename)
        download = request.GET.get("download")
        if download:
            content = "attachment; filename='%s'" %(filename)
        response['Content-Disposition'] = content
        return response
    return HttpResponse("Not found")

urls.py

url(r'^reports_pdf/(?P<id>\d+)/(?P<idsys>\d+)/$', views.GeneratePDF.as_view())

但我收到错误:方法不允许(GET) 你有什么建议吗?感谢&#39; S

(我跟着这个:Render_to_PDF_in_Django

1 个答案:

答案 0 :(得分:1)

您收到此错误是因为您继承了View类,默认情况下未实现get()方法。这意味着您的视图不知道如何处理GET请求,并引发错误。要解决问题,您需要为get()类定义GeneratePDF方法。只有在此GeneratePDF之后才能处理GET个请求。另外View类不提供get_context_data方法,您可以删除此context = super(GeneratePDF, self).get_context_data(*args,**kwargs)并将其替换为简单上下文定义为空字典context={}

class GeneratePDF(View):
    def get(self, *args,**kwargs):
        template = get_template('myreports.html')
        context = {}
        context['reports'] = Reports.objects.all().values('id','idsys','itemcode','ordercode').filter(id=self.kwargs['id'],idsys=self.kwargs['idsys'])

        html = template.render(context)
        pdf = render_to_pdf('myreports.html', context)
        if pdf:
            response = HttpResponse(pdf, content_type='application/pdf')
            filename = "myreports_%s.pdf" %("1")
            content = "inline; filename='%s'" %(filename)
            download = request.GET.get("download")
            if download:
                content = "attachment; filename='%s'" %(filename)
            response['Content-Disposition'] = content
            return response
        return HttpResponse("Not found")