将自定义操作添加到Django管理主页?

时间:2021-09-03 07:23:48

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.

我想为我正在设计的django可重用应用程序添加自定义操作,但是我想在管理站点的首页上放置一个指向该操作的链接,而不是特定于单个模型。

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?

是否可以轻松地在Django的主页中添加链接或站点范围的操作,还是重写模板?

2 个解决方案

#1


0  

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

是否可以轻松地将链接或站点范围的操作添加到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.

是的,这是覆盖模板(AFAIK)的问题。但简单而重要的模板并不是相互排斥的。

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):

对于任何到达这里的人,因为他们只是想要简单〜标准的方式来实际“添加自定义动作[即链接]到Django主页”(在django 1.9中测试):

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

给定一个名为'myproj'的django项目,你当前工作的目录是项目目录:

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.

从index.html模板中的以下内容开始,您的网站将保持不变。然后在这些块中放置链接以满足您的需要。

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

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

#2


0  

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"

#1


0  

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

是否可以轻松地将链接或站点范围的操作添加到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.

是的,这是覆盖模板(AFAIK)的问题。但简单而重要的模板并不是相互排斥的。

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):

对于任何到达这里的人,因为他们只是想要简单〜标准的方式来实际“添加自定义动作[即链接]到Django主页”(在django 1.9中测试):

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

给定一个名为'myproj'的django项目,你当前工作的目录是项目目录:

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.

从index.html模板中的以下内容开始,您的网站将保持不变。然后在这些块中放置链接以满足您的需要。

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

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

#2


0  

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"