Django 管理定制

发布于 2024-11-03 14:30:35 字数 368 浏览 8 评论 0原文

我正在设计一个管理界面,邀请邮件将发送给用户。我的邀请函模型已准备好在我的邀请管理界面中,我可以看到我添加的用户,管理员可以为其发送电子邮件邀请。 到目前为止的管理界面

现在我想对此进行一些自定义。我想为每一行添加一个 SEND 按钮,该按钮实际上会向该用户发送电子邮件。发送邮件功能等都已准备就绪。我不知道如何自定义此管理模板以添加 send 按钮。有人可以帮忙吗?或者至少为我指出正确的方向...

PS:它不必是发送按钮,它可以是“操作”下拉列表的一部分,我可以在其中为选定的用户联合发送电子邮件。

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. admin interface so far

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...

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

皇甫轩 2024-11-10 14:30:35

关于每行的发送按钮,您可以为您的模型(或 ModelAdmin)提供一个新函数,该函数返回指向您的视图的相应 HTML(或调用相应的 AJAX 函数)。只需将您的函数添加到 ModelAdmin 的“list_display”中,并确保 HTML 标签不会被转义:

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 作为参数:

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
    ]

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

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
    ]
朱染 2024-11-10 14:30:35

我下面的解决方案是在管理界面中添加“发送邀请”操作

“发送邀请”操作

您可以参考 django admin-actions 文档 此处
您的 admin.py 应该如下所示:

from django.contrib import admin
from myapp.models import MyModel
from django.core.mail import send_mail

class MyModelAdmin(admin.ModelAdmin):
    actions = ['send_invite']

    def send_invite(self, request, queryset):
        # the below can be modified according to your application.
        # queryset will hold the instances of your model
        for profile in queryset:
            send_email(subject="Invite", message="Hello", from_eamil='[email protected]', recipient_list=[profile.email]) # use your email function here
   send_invite.short_description = "Send invitation"

admin.site.register(MyModel, MyModelAdmin)

我尚未测试此代码,但它几乎是您所需要的。希望这有帮助。

My solution below is for adding the "send invite" action in admin interface

"Send Invite" action

You can refer to the django admin-actions documentation here.
Here is what your admin.py should look like:

from django.contrib import admin
from myapp.models import MyModel
from django.core.mail import send_mail

class MyModelAdmin(admin.ModelAdmin):
    actions = ['send_invite']

    def send_invite(self, request, queryset):
        # the below can be modified according to your application.
        # queryset will hold the instances of your model
        for profile in queryset:
            send_email(subject="Invite", message="Hello", from_eamil='[email protected]', recipient_list=[profile.email]) # use your email function here
   send_invite.short_description = "Send invitation"

admin.site.register(MyModel, MyModelAdmin)

I have not tested this code, but it is pretty much what you need. Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文