在django中提供pdf文件以供下载时出错

时间:2012-11-30 03:23:28

标签: python django download

这是我的网址

url(r'download/$', views.DownloadView.serve_pdf, name="pdfdata-ddl" )

这是我的观点

class DownloadView():

    def serve_pdf(request):
        #pdf_data = magically_create_pdf()

        response = HttpResponse(mimetype='application/pdf')
        response['Content-Disposition'] = 'attachment; filename="/test/angular.pdf"'
        return response

错误就是这个

unbound method serve_pdf() must be called with DownloadView instance as first argument (got WSGIRequest instance instead)

1 个答案:

答案 0 :(得分:2)

您需要传入self作为方法的第一个参数:

class DownloadView(object):
    def serve_pdf(self, request):
        #pdf_data = magically_create_pdf()

        response = HttpResponse(mimetype='application/pdf')
        response['Content-Disposition'] = 'attachment; filename="/test/angular.pdf"'
        return response

[编辑] 抱歉,我忘了提及你还需要在URL定义中实例化该类:

url(r'download/$', views.DownloadView().serve_pdf, name="pdfdata-ddl" )

要使用Python创建PDF,请查看Pisa