Problem using Django admin Actions with intermediate pages

Viewed 9531

I added an admin action send_EMAIL through admin.py. When admin uses the send_EMAIL action for selected users I want it to show an intermediate page with all selected users and ask for confirmation. In my case, it asks for confirmation but when I click on the "send Email" button nothing happens, and I get returned to the change_list view without the send_EMAIL action getting called.

Admin.py

class MyUserAdmin(UserAdmin):
    list_display = ['username', 'email', 'first_name', 'last_name', 'is_active', staff]
    list_filter = ['groups', 'is_staff', 'is_superuser', 'is_active']
    actions = ['send_EMAIL']

    
    def send_EMAIL(self, request, queryset):
        from django.core.mail import send_mail
        if 'apply' in request.POST:
            for i in queryset:
                if i.email:
                    send_mail('Subject here', 'Here is the message.', 'from@example.com',[i.email], fail_silently=False)
                else:
            self.message_user(request, "Mail sent successfully ")
        else:
            from django.http import HttpResponse
            from django.template import RequestContext, loader
            t = loader.get_template('admin/send_mail.html')
            c = RequestContext(request, {'articles': queryset})
            return HttpResponse(t.render(c),)
            
    
    
admin.site.unregister(User)
admin.site.register(User, MyUserAdmin)

templates/send_mail.html

{% extends "admin/base_site.html" %}

{% block content %}


<form action="" method="post">{% csrf_token %}

    <p>The mail will be send to following users:</p>

    <ul>{{ articles|unordered_list }}</ul>

    <input type="hidden" name="action" value="send_EMAIL" />
    <input type="submit" name="apply" value="Send Email" />
</form>

{% endblock %} 
1 Answers
Related