简体   繁体   English

Django创建PDF

[英]Django creating pdfs

Im trying to create a simple pdf document using my Django application as per this link 我试图按照此链接使用我的Django应用程序创建一个简单的pdf文档

https://docs.djangoproject.com/en/dev/howto/outputting-pdf/

The only change is that I'm creating it using a POST request through Jquery... 唯一的变化是我正在通过Jquery使用POST请求创建它...

jquery jQuery的

$("#exportpdf").click(function(){
            //alert('Hi' + '{{request.session.sessionid}}')
            $.ajax({
            async:false
            ,url:'/createpdf'
            ,type: 'POST'
            ,data: {session_id: '{{request.session.sessionid}}'}
            ,error: function(msg){
                alert("Call to Create pdf failed")
            }

        })
        })

views.py views.py

@csrf_exempt
def createpdf(request):
    response = HttpResponse(content_type='application/pdf')
    response['Content-Disposition'] = 'attachment; filename="somefilename.pdf"'
    p = canvas.Canvas(response)
    p.drawString(100, 100, "Hello world.")
    p.showPage()
    p.save()
    return response

Problem: 问题:

Using the above code when I click exportpdf id(as a post request), the pdf document is not opening(no action is seen/no errors in webserver logs) but when I access the /createpdf link directly as a GET request I can see the pdf document opening properly... 当我单击exportpdf id(作为发布请求)时,使用以上代码,pdf文档未打开(未发现任何操作/网络服务器日志中没有错误),但是当我直接作为GET请求访问/ createpdf链接时,我可以看到pdf文件正确打开...

Can't I have a post request and open a pdf document? 我不能提出要求并打开pdf文档吗? Should it only be a GET request? 应该只是GET请求吗?

You are missing your success callback in your ajax call: 您在ajax调用中缺少成功回调:

http://api.jquery.com/jquery.ajax/ http://api.jquery.com/jquery.ajax/

I would add a .done() handler 我会添加一个.done()处理函数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM