简体   繁体   English

Django使用按钮渲染为PDF

[英]Django render to pdf with button

I have def that render to pdf with action on django-admin. 我有对django-admin采取行动的def渲染为pdf文件。

def Print(self, request, obj):
    data = {
            'obj':obj
    }
    pdf = render_to_pdf('daa/imprimir/pdf.html', data)
    if pdf :
        response = HttpResponse(pdf, content_type='application/pdf')
        filename ="Avaria_%s.pdf" %("123451231")
        content = "inline; filename='%s'" %(filename)
        response['Content-Disposition'] = content
        download = request.GET.get("download")
        if download:
                content = "attachment; filename='%s'" %(filename)
        response['Content-Disposition'] = content
        return  response
    return HttpResponse("Not found")

and on my actions I have: 根据我的动作,我有:

class ModelAdmin(admin.ModelAdmin):
    actions = [Print]

and it is working all good, I select what objects I want to render and in my html I have cicles that make a list of all fields I want of those obj's. 而且一切正常,我选择了要渲染的对象,并且在html ,有cicle列出了我想要的所有obj字段。

But right now I don't want to render to pdf a list. 但是现在我不想将列表渲染为pdf。 I want to render only 1 obj. 我只想渲染1个obj。 So I create a custom button to do that. 因此,我创建了一个自定义按钮来做到这一点。

http://prntscr.com/muijhl http://prntscr.com/muijhl

So when I click on button I want to render to pdf the obj which is open. 因此,当我单击按钮时,我想将打开的obj渲染为pdf。 I don't know what I need to do to take my def and but inside of button 我不知道该如何做才能取得定义,但是在按钮内部

For how to hook this code as a view with it's own url, there's a perfect example in the official doc (but you have to know what to look for to find it) 有关如何使用自己的网址将该代码作为视图进行挂钩的方法,官方文档中有一个完美的示例 (但您必须知道要查找的内容)

Then you'll have to override your change_form template to add the button/link pointing to this url. 然后,您将不得不覆盖change_form模板以添加指向该URL的按钮/链接。

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

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