How to add button next to Add User button in Django Admin Site(如何在 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.
推荐答案
- 在您的模板文件夹中创建一个模板:admin/YOUR_APP/YOUR_MODEL/change_list.html
把这个放到那个模板里
- Create a template in you template folder: admin/YOUR_APP/YOUR_MODEL/change_list.html
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
享受;)
这篇关于如何在 Django 管理站点中的“添加用户"按钮旁边添加按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 Django 管理站点中的“添加用户"按钮旁边添加按钮
- 如何将一个类的函数分成多个文件? 2022-01-01
- 沿轴计算直方图 2022-01-01
- padding='same' 转换为 PyTorch padding=# 2022-01-01
- 分析异常:路径不存在:dbfs:/databricks/python/lib/python3.7/site-packages/sampleFolder/data; 2022-01-01
- 如何在 Python 的元组列表中对每个元组中的第一个值求和? 2022-01-01
- python-m http.server 443--使用SSL? 2022-01-01
- pytorch 中的自适应池是如何工作的? 2022-07-12
- 如何在 python3 中将 OrderedDict 转换为常规字典 2022-01-01
- 使用Heroku上托管的Selenium登录Instagram时,找不到元素';用户名'; 2022-01-01
- python check_output 失败,退出状态为 1,但 Popen 适用于相同的命令 2022-01-01
