Problem using Django admin Actions with intermediate pages

后端 未结 1 798
走了就别回头了
走了就别回头了 2020-12-15 07:49

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

相关标签:
1条回答
  • 2020-12-15 08:36

    I found an easy way to do it. It worked for me... I hope it helps:

    What you need to do is to "pass" the selected items to the confirmation page and include them in the form as well as including the <input type="hidden" name="action" value="admin_action" /> so that django admin knows that it should still call an admin action. The post is just to know whether to process the query set or render the confirmation page.

    # Write your admin action.
    # IMPORTANT: Note the context passed to TemplateResponse
    
    from django.contrib.admin import helpers
    from django.template.response import TemplateResponse
    
    class MyModelAdmin(admin.ModelAdmin):
        def admin_action(self, request, queryset):
            if request.POST.get('post'):
                # process the queryset here
            else:
                context = {
                    'title': _("Are you sure?"),
                    'queryset': queryset,
                    'action_checkbox_name': helpers.ACTION_CHECKBOX_NAME,
                }
                return TemplateResponse(request, 'path/to/template.html',
                    context, current_app=self.admin_site.name)
    
    # The template
    {% extends "admin/base_site.html" %}
    {% load i18n l10n %}
    
    {% block content %}
    <form action="" method="post">{% csrf_token %}
        <p>The following videos will be accepted:</p>
    
        <ul>{{ queryset|unordered_list }}</ul>
    
        <div>
        {% for obj in queryset %}
        <input type="hidden" name="{{ action_checkbox_name }}" value="{{ obj.pk|unlocalize }}" />
        {% endfor %}
        <input type="hidden" name="action" value="admin_action" />
        <input type="hidden" name="post" value="yes" />
        <input type="submit" value="{% trans "Yes, I'm sure" %}" />
        </div>
    </form>
    {% endblock %}
    
    0 讨论(0)
提交回复
热议问题