0

So I want to add an extra button in my Fun model's changelist_view which when pressed, performs a function and redirects me to a new page. I don't want to have this function for every app nor do I want for every model, only the Fun model. And I will be using Django Admin to view this changelist, just a side note.

Can anyone suggest anything? Thanks.

  • You can use [admin actions](https://docs.djangoproject.com/en/2.2/ref/contrib/admin/actions/) – Pedram Nov 30 '19 at 11:40
  • Thank you for your comment Pedram Parsian. I've already considered that but I was looking for a physical `button` that I could click on (Like a button next to the add button) to perform my action. – Rocky Cipher Nov 30 '19 at 13:07
  • So you can _customize_ the `change_list_template`, add your button and link that to your view. Basically, you should add something like `My Button` – Pedram Nov 30 '19 at 13:09
  • Wouldn't that defeat the purpose of using Django Admin? Could you show a working example in the answers? – Rocky Cipher Nov 30 '19 at 19:21
  • Is it possible to add override the template for only a specific Model or something? – Rocky Cipher Nov 30 '19 at 19:28

1 Answers1

2

Codes in fun/admins.py

from django.contrib import admin
from .models import Fun


@admin.register(Fun)
class FunAdmin(admin.ModelAdmin):
    change_list_template = 'fun/admin_changelist.html'

and for the fun/admin_changelist.html template:

{% extends "admin/change_list.html" %}
{% load i18n admin_static admin_list %}

{% block result_list %}
  <a href="{% url 'your-url-name here' %}">The custom button</a>
  {% if action_form and actions_on_top and cl.full_result_count %}{% admin_actions %}{% endif %}
  {% result_list cl %}
  {% if action_form and actions_on_bottom and cl.full_result_count %}{% admin_actions %}{% endif %}
{% endblock %}

Overriding the admin templates is perfectly fine, but replacing them (completely) is not recommended; So we will just override the part that we need (here I assume your button will be located in the top of the list, so I only override the result_list block)

Then you should have url with name="your-url-name here" that is connected to a view.

Check the docs for more details

Pedram
  • 3,485
  • 3
  • 17
  • 32
  • Aaah! That neat. Thank you Pedram. Also, how could I perform an action when I click the button? Like let's say I want to `print("Button Pressed")` when the button is pressed. How would I do that? – Rocky Cipher Dec 01 '19 at 11:37
  • 2
    When the button is clicked, it will run the code inside the associated view for a url named `your-url-name here` (see template code). So you can put your code there. – Pedram Dec 01 '19 at 11:39
  • 1
    Let me know if you can't get it working as you want. – Pedram Dec 01 '19 at 11:47
  • Thank you Pedram, you've helped me a lot. But here's just a single tad thing I don't understand. Since I only want to execute a function and stay on the same page, I don't understand how work this out. Do I create a view, put my function in it, as soon as it's complete, redirect back to my page? – Rocky Cipher Dec 01 '19 at 14:30
  • 1
    @AnthonyMartinez Yes, you shoud redirect back to the same page - check [this](https://stackoverflow.com/q/12758786/9733868); and if you don't want to see the page _refreshing_ when you click the button, you can use **ajax calls**. – Pedram Dec 01 '19 at 16:17
  • And there we go. You have saved my soul. Thank You for your time Pedram – Rocky Cipher Dec 01 '19 at 19:26