简体   繁体   中英

Django render to pdf with button

I have def that render to pdf with action on django-admin.

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.

But right now I don't want to render to pdf a list. I want to render only 1 obj. So I create a custom button to do that.

http://prntscr.com/muijhl

So when I click on button I want to render to pdf the obj which is open. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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