Python 对中间页面使用Django管理操作时出现问题
我通过admin.py添加了一个管理操作发送电子邮件。当管理员为所选用户使用send_EMAIL操作时,我希望它显示一个包含所有所选用户的中间页面,并请求确认。在我的情况下,它要求确认,但当我点击发送电子邮件按钮时,什么也没有发生,我返回到更改列表视图,而没有调用发送电子邮件操作 管理员 模板/send_mail.htmlPython 对中间页面使用Django管理操作时出现问题,python,django,django-admin,django-templates,Python,Django,Django Admin,Django Templates,我通过admin.py添加了一个管理操作发送电子邮件。当管理员为所选用户使用send_EMAIL操作时,我希望它显示一个包含所有所选用户的中间页面,并请求确认。在我的情况下,它要求确认,但当我点击发送电子邮件按钮时,什么也没有发生,我返回到更改列表视图,而没有调用发送电子邮件操作 管理员 模板/send_mail.html 我找到了一个简单的方法。它对我有用。。。我希望这有助于: 您需要做的是将所选项目传递到确认页面,并将它们包括在表单中,同时也包括,以便django管理员知道它仍然应该调用管理
我找到了一个简单的方法。它对我有用。。。我希望这有助于: 您需要做的是将所选项目传递到确认页面,并将它们包括在表单中,同时也包括,以便django管理员知道它仍然应该调用管理操作。文章只是想知道是处理查询集还是呈现确认页面
# 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 %}
queryset来自哪里?这个urlconf条目是什么?我指的是实现中间页面>我真的不太了解Django,所以我发布了这个问题。这个{queryset%}中的obj的%for}东西很有效。但对我来说,这是一种黑魔法。有人知道它为什么工作吗?它添加了一个隐藏的表单输入,列出了你想要更改的所有对象。什么是黑魔法,它为什么在没有它的情况下在第一页工作。明白了!我使用的是request.POST.getlistadmin.ACTION\u CHECKBOX\u名称,它只获取页面上选择的100个项目。Queryset发现选择了“全选”。obj.pk | unlocalize出现错误:django.template.exceptions.TemplateSyntaxError:筛选器无效:“unlocalize”
{% 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 %}
# 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 %}