如何将自定义视图添加到 django 管理界面? [英] How to add custom view to django admin interface?

查看:22
本文介绍了如何将自定义视图添加到 django 管理界面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 django 管理界面如下所示:

现在我想添加一个与模型不对应的视图.

我可以覆盖上面页面的模板并添加一个自定义链接.但我认为这看起来很难看.

覆盖admin/index.html的示例:

{% extends "admin/index.html" %}{% 块内容 %}{{ block.super }}<div class="app-sonstiges 模块">……</div>{% 端块 %}

但也许有一种官方方法可以将自定义视图添加到管理界面?

在我的例子中,我想提供一个可以执行 tcptraceroute 到远程服务器的表单.我的应用的管理员需要这个.

我使用了相同的 html 标签.现在链接tcptraceroute"看起来不错,但不幸的是消息下移了:

有没有办法获得像屏幕截图中的Sontiges ... tcptraceroute"这样的自定义部分,而无需向下移动最新操作?

这是 html 结构的样子.我的 <div class="app-sonstiges"> 在 content-main 之下:

解决方案

这里有 3 个选项:

使用提供菜单的第三方包

这很简单,有一些很好的软件包支持管理员菜单,以及一些将您的项目添加到菜单的方法.

第 3 方包的一个示例是 django-admin-tools.缺点是有点难学.另一种选择是使用 django-adminplus 但在撰写本文时,它不支持 Django 2.2.

使用 django 管理模板进行黑客攻击

您始终可以扩展管理模板并覆盖您想要的部分,如@Sardorbek 回答中所述,您可以从管理模板中复制一些部分并按照您想要的方式更改它们.

使用自定义管理站点

如果您的视图应该只对管理站点起作用,那么您可以扩展 adminsite(也许

My django admin interface looks like this:

Now I would like to add a view which does not correspond to a model.

I could overwrite the template of above page and add a custom link. But I think this would look ugly.

Example for overwriting admin/index.html:

{% extends "admin/index.html" %}

{% block content %}

{{ block.super }}
<div class="app-sonstiges module">
   ....
</div>
{% endblock %}

But maybe there is an official way to do add a custom view to the admin interface?

In my case I want to provide a form which can execute tcptraceroute to a remote server. The admin of my app needs this.

I used the same html tags. Now the link "tcptraceroute" looks nice, but unfortunately the messages moved down:

Is there a way to get a custom part like "Sontiges ... tcptraceroute" like in the screenshot, without moving the latest actions down?

Here is how the html structure looks like. My <div class="app-sonstiges"> is below content-main:

解决方案

You have 3 options here:

Using third-party packages which provide menu

This is pretty straight forward, there are some good packages out there which support menu for admin, and some way to add your item to the menu.

An example of a 3rd party package would be django-admin-tools. The drawback is that it is a bit hard to learn. Another option would be to use django-adminplus but at the time of writing this, it does not support Django 2.2.

Hacking with django admin templates

You can always extend admin templates and override the parts you want, as mentioned in @Sardorbek answer, you can copy some parts from admin template and change them the way you want.

Using custom admin site

If your views are supposed to only action on admin site, then you can extend adminsite (and maybe change the default), beside from adding links to template, you should use this method to define your admin-only views as it's easier to check for permissions this way.

Considering you already extended adminsite, now you can use the previous methods to add your link to template, or even extend get_app_list method, example:

class MyAdminSite(admin.AdminSite):
    def get_app_list(self, request):
        app_list = super().get_app_list(request)
        app_list += [
            {
                "name": "My Custom App",
                "app_label": "my_test_app",
                # "app_url": "/admin/test_view",
                "models": [
                    {
                        "name": "tcptraceroute",
                        "object_name": "tcptraceroute",
                        "admin_url": "/admin/test_view",
                        "view_only": True,
                    }
                ],
            }
        ]
        return app_list

You can also check if current staff user can access to module before you show them the links. It will look like this at then end:

这篇关于如何将自定义视图添加到 django 管理界面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆