1

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.

2 Answers 2

4

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
    ]
3
  • cool! this is pretty good. when the admin clicks "send" how would I go about adding functionality to this? basically upon clicking send the entire row has to be sent to my send_email_view which sends the email... Apr 27, 2011 at 12:22
  • sorry, just re-read your answer. So you suggest the ajax route which from javascript sends the entire row to my send_email_view. Are there any other ways ? Apr 27, 2011 at 12:24
  • Sorry didn't check SO for a while... Hmmm... you could add a submit button to each row - putting the corresponding row's object id in its name - and then check which button was clicked instead of looking which checkboxes were ticked. So basically like the action but reading a single object from the button name instead of the checkboxes. Jun 3, 2011 at 10:23
4

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.