Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a custom button to Django admin

I want to add a custom button right next to the SAVE button in my admin change form. I tried to add a button in submit_line.html

<input type="submit" action = "admin/{{id??}}" value="show PDF" name="show PDF{{ onclick_attrib }}/>

But, it doesn't redirect to my given page and I don't know how to pass the current id. Actually, I dont think it's a good idea to change submit_line.html anyways, because I only want it in one model. Is there a better solution for that?

like image 267
kiwi541 Avatar asked Aug 30 '25 18:08

kiwi541


1 Answers

I think you can add your own logic. Here you check app verbose name, to show that button only in your app:

{% if opts.verbose_name == 'Your app verbose name' %}<input type="submit" value="{% trans 'Show PDF' %}" name="_show_pdf" {{ onclick_attrib }}/>{% endif %}

and in your admin.py:

class YourAdmin(admin.ModelAdmin):
    def response_change(self, request, obj):
        if '_continue' in request.POST:
            return HttpResponseRedirect(obj.get_admin_url())
        elif '_show_pdf' in request.POST:
            # Do some stuf here( show pdf in your case)
like image 187
wolendranh Avatar answered Sep 02 '25 06:09

wolendranh