如何在 django 管理员更改列表视图页面中添加按钮 [英] How can I add a button into django admin change list view page

查看:25
本文介绍了如何在 django 管理员更改列表视图页面中添加按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的模型的模型列表视图中的添加"按钮旁边添加一个按钮,然后创建一个视图函数,我将在其中完成我的工作,然后将用户重定向回列表视图.

I would like to add a button next to "add" button in list view in model for my model and then create a view function where I will do my stuff and then redirect user back to list view.

我已经检查了如何重载管理模板,但我仍然不知道,我应该把我的视图函数放在哪里我会做我的事情,以及如何将该视图注册到管理 url.

I've checked how to overload admin template, but I still dont know, where should I put my view function where I will do my stuff, and how can I register that view into admin urls.

还有关于安全性的问题.我希望在 admin 中执行该操作,因此如果您未登录,则无法使用它.

There is also question about security. I would like to have that action inside admin, so if u r not logged in, u cannot use it.

我找到了这个,但我不知道它是否正确:http://www.stavros.io/posts/how-to-extend-the-django-admin-site-with-custom/

I've found this, but I don't know if it's the right way: http://www.stavros.io/posts/how-to-extend-the-django-admin-site-with-custom/

推荐答案

当多个应用程序提供相同版本的不同版本时资源(模板、静态文件、管理命令、翻译)、INSTALLED_APPS 中首先列出的应用程序具有优先权.- 关于 INSTALLED_APPS 的 Django 文档

确保您的应用列在 INSTALLED_APPS 中的 'django.contrib.admin' 之前.

Make sure your app is listed before 'django.contrib.admin' in INSTALLED_APPS.

在以下目录之一中创建 change_list.html 模板:

Create a change_list.html template in one of the following directories:

# Template applies to all change lists.
myproject/myapp/templates/admin/change_list.html      

# Template applies to change lists in myapp.
myproject/myapp/templates/admin/myapp/change_list.html  

# Template applies to change list in myapp and only to the Foo model.
myproject/myapp/templates/admin/myapp/foo/change_list.html  

模板应该被自动拾取,但如果它不在上面列出的路径之一上,您也可以通过管理模型属性指向它:

The template should be picked up automatically, but in case it is not on one of paths listed above, you can also point to it via an admin model attribute:

class MyModelAdmin(admin.ModelAdmin):

    #... 
    change_list_template = "path/to/change_list.html"

您可以在 path/to/your/site-packages/django/contrib/admin/templates/admin/change_list.html 中查找原始 change_list.html 的内容.该其他答案 还向您展示了如何格式化模板.Nikolai Saiko 向您展示了如何使用 'extends' 和 'super' 覆盖相关部分.总结:

You can lookup the contents of the original change_list.html it lives in path/to/your/site-packages/django/contrib/admin/templates/admin/change_list.html. The other answer also shows you how to format the template. Nikolai Saiko shows you how to override the relevant parts using 'extends' and 'super'. Summary:

{% extends "admin/change_list.html" %} {% load i18n %} 
{% block object-tools-items %}
    {{ block.super }}
    <li>
        <a class="historylink" href="...">My custom admin page</a>
    </li>
{% endblock %}

让我们用 url 填充 href="...".admin url 名称位于命名空间admin"中,可以这样查找:

Let's fill href="..." with an url. The admin url names are in the namespace 'admin' and can be looked up like this:

{% url 'admin:custom_view' %}

当您向 change_form.html 添加按钮时,您可能希望传入当前对象 ID:

When you are adding a button to change_form.html you maybe want to pass in the current object id:

{% url 'admin:custom_view' original.pk %}

现在创建一个自定义视图.这可以是常规视图(就像您网站上的其他页面一样)或 admin.py 中的自定义管理视图.ModelAdmin 上的 get_urls 方法以与 URLconf 相同的方式返回要用于该 ModelAdmin 的 URL.因此,您可以按照 URL 调度程序中的说明扩展它们:

Now create a custom view. This can be a regular view (just like other pages on your website) or a custom admin view in admin.py. The get_urls method on a ModelAdmin returns the URLs to be used for that ModelAdmin in the same way as a URLconf. Therefore you can extend them as documented in URL dispatcher:

class MyModelAdmin(admin.ModelAdmin):
    def get_urls(self):
        urls = super(MyModelAdmin, self).get_urls()
        my_urls = patterns('',
            url(r'^my_view/$', self.my_view, name="custom_view")
        )
        return my_urls + urls

    def my_view(self, request):
        # custom view which should return an HttpResponse
        pass

    # In case your template resides in a non-standard location
    change_list_template = "path/to/change_list.html"

阅读有关如何在 ModelAdmin 中设置视图权限的文档:https://docs.djangoproject.com/en/1.5/ref/contrib/admin/#django.contrib.admin.ModelAdmin.get_urls

Read the docs on how to set permissions on a view in ModelAdmin: https://docs.djangoproject.com/en/1.5/ref/contrib/admin/#django.contrib.admin.ModelAdmin.get_urls

您可以保护您的视图,并且只向具有员工身份的用户授予访问权限:

You can protect your view and only give access to users with staff status:

from django.contrib.admin.views.decorators import staff_member_required

@staff_member_required
def my_view(request):
    ...

您可能还想检查 request.user.is_active 并处理非活动用户.

You might also want to check request.user.is_active and handle inactive users.

更新:利用框架并尽可能少地进行自定义.很多时候操作可能是一个不错的选择:https://docs.djangoproject.com/en/1.5/ref/contrib/admin/actions/

Update: Take advantage of the framework and customise as little as possible. Many times actions can be a good alternative: https://docs.djangoproject.com/en/1.5/ref/contrib/admin/actions/

更新 2:我删除了一个 JS 示例以注入一个按钮客户端.如果需要,请参阅修订版.

Update 2: I removed a JS example to inject a button client side. If you need it, see the revisions.

这篇关于如何在 django 管理员更改列表视图页面中添加按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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