Pedro Mariz
Pedro Mariz

Reputation: 161

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

Upvotes: 0

Views: 599

Answers (1)

bruno desthuilliers
bruno desthuilliers

Reputation: 77912

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.

Upvotes: 2

Related Questions