I want to add custom buttons to the add/change form at the administration interface. By default, there are only three:
-
Save and add another
-
Save and continue editing
-
Save
I have created some custom methods in my forms.py file, and I want to create buttons to call these methods. I have used the snippet http://djangosnippets.org/snippets/1842/, but it’s not exactly what I want. This one allows to create buttons and call methods from the admin.py file and not forms.py.
Is there a way to do that?
This is my admin.py code:
class CategoryAdmin(admin.ModelAdmin):
prepopulated_fields = { "alias": ("title",) }
form = CategoryForm
admin.site.register(Category, CategoryAdmin)
And my forms.py code:
class CategoryForm(forms.ModelForm):
"""
My attributes
"""
def custom_method(self):
print("Hello, World!")
How do I create a button that calls "custom_method()"?
You can override
admin/change_form.html. Copy the version incontrib.admin.templatesinto your project. Mine ismyproject/templates/admin/change_form.html, but you could use/myproject/myapp/templates/admin/change_form.html.Next, edit the copy and change the two references to the existing template tag,
{% submit_row %}, to point to your own template tag,{% my_template_tag %}.Base your template tag on the
contrib.admin‘s{% submit_row %}, but edit the HTML template to contain any extra buttons you want to display.