使用带有中间页面的 Django 管理操作的问题 [英] Problem using Django admin Actions with intermediate pages

查看:18
本文介绍了使用带有中间页面的 Django 管理操作的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Admin.py

class MyUserAdmin(UserAdmin):list_display = ['用户名', 'email', 'first_name', 'last_name', 'is_active', 工作人员]list_filter = ['groups', 'is_staff', 'is_superuser', 'is_active']动作 = ['send_EMAIL']def send_EMAIL(self, request, queryset):从 django.core.mail 导入 send_mail如果在 request.POST 中申请":对于查询集中的 i:如果 i.email:send_mail('Subject here', 'Here is the message.', 'from@example.com',[i.email], fail_silently=False)别的:self.message_user(请求,邮件发送成功")别的:从 django.http 导入 HttpResponsefrom django.template import RequestContext, loadert = loader.get_template('admin/send_mail.html')c = RequestContext(request, {'articles': queryset})返回 HttpResponse(t.render(c),)admin.site.unregister(用户)admin.site.register(用户,MyUserAdmin)

templates/send_mail.html

{% 扩展admin/base_site.html"%}{% 块内容 %}<form action=""方法=发布">{% csrf_token %}<p>邮件将发送给以下用户:</p><ul>{{ 文章|unordered_list }}</ul><输入类型=隐藏"名称=动作"值=发送电子邮件"/><输入类型=提交"名称=申请"值=发送电子邮件"/></表单>{% 结束块 %}

解决方案

我找到了一个简单的方法.它对我有用......我希望它有帮助:

您需要做的是将选定的项目传递"到确认页面并将它们包含在表单中以及包含<input type="hidden" name="action" value="admin_action"/> 以便 Django 管理员知道它仍然应该调用 admin action.post 只是知道是处理查询集还是渲染确认页面.

# 编写您的管理操作.# 重要:注意传递给 TemplateResponse 的上下文从 django.contrib.admin 导入助手从 django.template.response 导入 TemplateResponse类 MyModelAdmin(admin.ModelAdmin):def admin_action(self, request, queryset):如果 request.POST.get('post'):# 在这里处理查询集别的:上下文 = {'title': _("你确定吗?"),查询集":查询集,'action_checkbox_name':helpers.ACTION_CHECKBOX_NAME,}返回 TemplateResponse(request, 'path/to/template.html',上下文,current_app=self.admin_site.name)# 模板{% 扩展 "admin/base_site.html" %}{% 加载 i18n l10n %}{% 块内容 %}<form action="" method="post">{% csrf_token %}<p>以下视频将被接受:</p><ul>{{ queryset|unordered_list }}</ul><div>{% for obj in queryset %}<input type="hidden" name="{{ action_checkbox_name }}" value="{{ obj.pk|unlocalize }}"/>{% 结束为 %}<input type="hidden" name="action" value="admin_action"/><input type="hidden" name="post" value="yes"/><input type="submit" value="{% trans "Yes, I'm sure" %}"/>

</表单>{% 结束块 %}

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 %} 

解决方案

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 %}

这篇关于使用带有中间页面的 Django 管理操作的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆