Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create custom buttons in admin change_form in Django

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()"?

like image 259
bnabilos Avatar asked Feb 12 '11 13:02

bnabilos


People also ask

How do I add a button to a Django project?

First of all, create a project and an app. In settings.py, add django_social_share as an app in project. Set up your app's urls. In app's urls.py:, render a view.

Can we customize Django admin panel?

The Django admin is a powerful built-in tool giving you the ability to create, update, and delete objects in your database using a web interface. You can customize the Django admin to do almost anything you want.

How customize Django admin CSS?

In your static directory, create a static/admin/css/base. css file. Paste in Django's default Admin CSS first, then add your customizations at the bottom. If you do this, be sure to put your app BEFORE django.

How do I make administrative superuser in Django?

Django's “Hello World” application Start the terminal by clicking on the “Run” button. Type python3 manage.py createsuperuser in the given terminal and press “Enter”. The system will ask for credentials, after which a superuser will be created. To run the server, we type the command python3 manage.py runserver 0.0.


2 Answers

One simple way I found to add buttons is to add another row for the custom buttons. Create an admin directory in your template dir based on your needs. For example I usually add buttons for specific models in a custom template. Make a "templates/admin/app/model/" directory.

Then add a file change_form.html.

{% extends "admin/change_form.html" %}
{% load i18n %}

{% block submit_buttons_bottom %}
    <div class="submit-row">
       <input type="button" value="{% trans 'Another Button' %}" name="_anotherbutton" />
    </div>

    {{ block.super }}
{% endblock %}

The code before the {{ block.super }} is inspired by the submit_line.html template used by the template tag {% submit_row %}. I prefer this method because is straightforward but you must live with another row of buttons.

enter image description here

like image 171
Karim N Gorjux Avatar answered Oct 08 '22 06:10

Karim N Gorjux


You can override admin/change_form.html. Copy the version in contrib.admin.templates into your project. Mine is myproject/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.

like image 19
Andy Baker Avatar answered Oct 08 '22 07:10

Andy Baker