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

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

问题描述

我的django管理界面如下:





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



我可以覆盖上一页的模板并添加一个自定义链接。



覆盖 admin / index.html

$的示例b
$ b

  {%扩展了 admin / index.html%} 

{%块内容%}

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

但是也许有一种官方的方式来添加自定义查看管理界面?



对于我来说,我想提供一个可以执行 tcptraceroute 的表单服务器。我的应用程序的管理员需要这个。



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





有没有办法像屏幕快照中那样获得自定义零件,例如 Sontiges ... tcptraceroute,而无需向下移动最新动作?



这是html结构的外观。我的< div class = app-sonstiges> 在content-main下面:



解决方案

您在这里有3个选择:



使用提供菜单的第三方程序包



这很简单,那里有一些支持管理员菜单的好的程序包,以及将菜单项添加到菜单的某种方式。



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



< h2>使用django管理模板进行攻击

您可以随时扩展管理模板并覆盖所需的部分,如@Sardorbek答案中所述,您可以从管理模板中复制某些部分并按照您想要的方式更改它们。



使用自定义管理网站



如果您的意见只应采取行动在管理网站上,则可以扩展管理网站(也许


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天全站免登陆