user764357
user764357

Reputation:

Add custom actions to Django admin home page?

I'm looking to add a custom action for a django reusable app I'm designing, however rather than being specific to a single model, I want to place a link to the action on the front page of the admin site.

Is it easily possible to add links or site-wide actions to the home page in Django, or is it a matter of overriding templates?

Upvotes: 10

Views: 2977

Answers (2)

SuReSh
SuReSh

Reputation: 1511

Create custom action method and call in actions list.

 class YourCustomClass(admin.ModelAdmin):
        model: YourModel
        actions = ['make_active', 'make_inactive',]

        def make_active(self, request, queryset):
            '''make_active from action'''
            queryset.update(is_active=True)
        make_active.short_description = "Make selected users as Active"

        def make_inactive(self, request, queryset):
            '''make_inactive from action'''
            queryset.update(is_active=False)
        make_inactive.short_description = "Make selected users as Inactive"

Upvotes: -1

spinkus
spinkus

Reputation: 8550

Is it easily possible to add links or site-wide actions to the home page in Django ...

links yes, Note sure what site-wide actions means.

or is it a matter of overriding templates?

Yep, it's a matter of overriding templates (AFAIK). But easy and overriding templates are not mutually exclusive.

For anyone who got here because they just want the easy ~standard way to actually "add custom actions [i.e. links] to Django home page" (tested in django 1.9):

Given a django project called 'myproj' and your current working dir is the project dir:

mkdir -p myproj/templates/admin/
edit myproj/templates/admin/index.html

Start with the following in index.html template and your site will be unchanged. Then fiddle about putting links in those blocks to suit your need.

{% extends "admin/index.html" %}
{% block content %}
{{ block.super }}
{% endblock %}

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

Upvotes: 2

Related Questions