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.
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