django admin actions 自定义actions view
django1.8
不多说,上代码
#coding=utf-8 from django.contrib import admin from common.defines import BMOBJ, YAdmin from models import * from django.utils.translation import ugettext_lazy as _ from django.contrib import messages # from django.http import HttpResponse, HttpResponseRedirect from django.shortcuts import render_to_response from django.template import RequestContext from gtwisted.utils import log from django import forms # Register your models here. def copy_player(modeladmin, request, queryset): form = None log.err("$"*50) if 'cancel' in request.POST: modeladmin.message_user(request, u'已取消') log.err("#"*50) return elif 'ok' in request.POST: modeladmin.message_user(request, u'已复制') log.err("!"*50) return HttpResponseRedirect(request.get_full_path()) if not form: try: form = modeladmin.data_src_form(initial={'_selected_action': request.POST.getlist(admin.ACTION_CHECKBOX_NAME)}) except Exception,e: log.err(e) return render_to_response('player_copy.html', {'objs': queryset, 'form': form, 'path':request.get_full_path(), 'action': 'copy_player', 'title': u'复制数据'}, context_instance=RequestContext(request) ) copy_player.short_description = u'复制' class PlayerAdmin(YAdmin): list_display = ('id','uid','nickname') search_fields = ('uid', 'nickname') actions = [copy_player] class data_src_form(forms.forms.Form): _selected_action = forms.CharField(widget=forms.MultipleHiddenInput)
player_copy.html内容为:
##############################
{% extends "admin/base_site.html" %}
{% block content %}
<form method="post" action="{{path}}">
{% csrf_token %}
{{ form }}
<p>
<input type="hidden" name="action" value="{{ action }}" />
<input type="submit" name="cancel" value="取消" />
<input type="submit" name="ok" value="确定"/>
</p>
</form>
<p>将批量修改以下所有对象</p>
<ul>
{% for obj in objs %}
<li>{{ obj }}</li>
{% endfor %}
</ul>
{% endblock %}
###############################