Django管理员自定义 [英] Django Admin Customizing
问题描述
我正在设计一个管理界面,邀请邮件将发送给用户。我的邀请模式准备好了在我的邀请管理界面中,我可以看到我的添加用户,管理员可以发送电子邮件邀请。
I am designing an admin interface where invite mails will be sent to users. My Invitation model is ready & in my invitation admin interface I am able to see my added users for which the admin can send email invites.
现在我要自定义这有点我想为每行添加一个 SEND
按钮,实际上会向该用户发送一封电子邮件。发送电子邮件功能等都准备好了。我不知道如何自定义此管理模板以添加发送
按钮。有人可以帮忙吗?或者至少指向我正确的方向...
now I want to customize this a bit. I want to add for each row a SEND
button which will actually send an email to that user. Sending email function etc. are all ready. I am not getting as to how I can customize this admin template to add a send
button. Can someone help ?? or atleast point me in the right direction...
PS:它不需要是发送按钮,它可以是动作下拉列表中可以联合发送电子邮件的所选用户。
P.S: it need not be a send button, it could be part of "action" dropdown where for the selected users I can jointly send emails.
推荐答案
关于每行的发送按钮,您可以给出model(或ModelAdmin)一个新函数,返回指向您的视图的对应HTML(或调用相应的AJAX函数)。只需将您的功能添加到ModelAdmin的list_display,并确保HTML标签不被转义:
Regarding the send button for each row, you can give your model (or ModelAdmin) a new function which returns the corresponding HTML pointing to your views (or calling corresponding AJAX functions). Just add your function to the ModelAdmin's "list_display" and make sure that HTML tags don't get escaped:
class MyModelAdmin(admin.ModelAdmin):
...
list_display = ('name', 'email', 'sender', 'send_email_html')
def send_email_html(self, obj):
# example using a javascript function send_email()
return '<a href="send_email(%s)">Send Now</a>' % obj.id
send_email_html.short_description = 'Send Email'
send_email_html.allow_tags = True
关于使用操作,将ModelAdmin中的actions定义为包含以modeladmin,request,queryset为参数的函数的列表:
Regarding the use of an action, define "actions" in your ModelAdmin as a list containing your function which takes modeladmin, request, queryset as parameters:
def send_email_action(modeladmin, request, queryset):
whatever_you_want_to_do_with_request_and_queryset
send_email.short_description = 'Send email'
class MyModelAdmin(admin.ModelAdmin):
...
actions = [
send_email_action
]
这篇关于Django管理员自定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!