如何在 Django 管理站点中的“添加用户"按钮旁边添加按钮

问题描述

我正在处理 Django 项目,我需要从 Django Admin 的用户屏幕中提取用户列表以使其表现出色.我将 actions 变量添加到我的示例类中,以便在每个用户的 id 之前获取 CheckBox.

I am working on Django Project where I need to extract the list of user to excel from the Django Admin's Users Screen. I added actions variable to my Sample Class for getting the CheckBox before each user's id.

class SampleClass(admin.ModelAdmin):
    actions =[make_published]

动作 make_published 已定义.现在我想在 Add user 按钮旁边添加另一个按钮,如图所示..但我不知道如何在不使用新模板的情况下实现这一点.我想使用该按钮将选定的用户数据打印到 Excel 中.谢谢,请指导我.

Action make_published is already defined. Now I want to append another button next to Add user button as shown in fig. . But I dont know how can I achieve this this with out using new template. I want to use that button for printing selected user data to excel. Thanks, please guide me.


解决方案

  1. 在您的模板文件夹中创建一个模板:admin/YOUR_APP/YOUR_MODEL/change_list.html
  2. 把这个放到那个模板里

  1. Create a template in you template folder: admin/YOUR_APP/YOUR_MODEL/change_list.html
  2. Put this into that template

{% extends "admin/change_list.html" %}
{% block object-tools-items %}

    {{ block.super }}

    <li>
        <a href="export/" class="grp-state-focus addlink">Export</a>
    </li>

{% endblock %}

  • YOUR_APP/admin.py中创建一个视图函数并用注解保护它

  • Create a view function in YOUR_APP/admin.py and secure it with annotation

    from django.contrib.admin.views.decorators import staff_member_required
    
    @staff_member_required
    def export(self, request):
    
        ... do your stuff ...
    
        return HttpResponseRedirect(request.META["HTTP_REFERER"])
    

  • 将新的 url 添加到 YOUR_APP/admin.py 到管理模型的 url 配置

  • Add new url into YOUR_APP/admin.py to url config for admin model

    from django.conf.urls import patterns, include, url
    
    class YOUR_MODELAdmin(admin.ModelAdmin):
    
        ... list def stuff ...
    
        def get_urls(self):
            urls = super(MenuOrderAdmin, self).get_urls()
            my_urls = patterns("",
                url(r"^export/$", export)
            )
            return my_urls + urls
    

  • 享受;)

    相关文章