Add button to Django admin index page

Viewed 1330

I want to add a site-wide action to Django admin, called "Synchronize SQL-KB". Site-wide button "Synchronize SQL-KB"

For that I have copied locally templates/admin/index.html file from Django and modified it as follows:

...
{% if app_list %}
    <table>
        <tr>
            <td>
                <a href="sync-sql-kb/">Synchronize SQL-KB</a>
            </td>
        </tr>
    </table>
    {% for app in app_list %}
    ...

So as you can see I want the link button to trigger a request to /admin/sync-sql-kb/.

How to register this URL now? Where and how to implement a handler function?

Note that this situation in my view differs from other SO questions in that the action is site-wide, rather than bound to a specific Django model.

1 Answers

You can write your own views & just register them alongside the admin URLs;

urlpatterns = [
    path('admin/sync-sql-kb/', myapp.urls),
    path('admin/', admin.site.urls),
]

Then you could just do; <a href="/admin/sync-sql-kb/">Synchronize SQL-KB</a>

Ensure your view does the correct checking for the user being staff or superuser, depending on what you need. Also consider that you can name your URLs and use reverse as normal for this.

I would recommend adding your link to the userlinks block of the admin templates as well. Read this for more information; https://cheat.readthedocs.io/en/latest/django/admin.html#customize-top-right-corner-of-admin-pages

Related