使用Django admin的问题使用中间页面的操作

Ans*_*hul 15 python django django-templates django-admin

我通过admin.py添加了管理员操作send_EMAIL.我希望当管理员对所选用户使用send_EMAIL操作时,它应该显示一个包含所有选定用户的中间页面并要求确认.在我的情况下,它要求确认但是当我点击"发送"时电子邮件"按钮没有任何反应,我返回到change_list视图,没有调用send_EMAIL操作.

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)
Run Code Online (Sandbox Code Playgroud)

模板/ 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 %} 
Run Code Online (Sandbox Code Playgroud)

对不起英语不好.Plz的帮助

Ger*_*ard 24

我发现了一种简单的方法.它对我有用......我希望它有所帮助:

您需要做的是将所选项目"传递"到确认页面并将其包含在表单中以及包括<input type="hidden" name="action" value="admin_action" />这样,以便django admin知道它仍然应该调用admin action.该post是只知道是否处理查询集或渲染确认页面.

# 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 %}
Run Code Online (Sandbox Code Playgroud)