I have a django model for which I want to add custom actions. Where in this action I need to add one intermediate page with a selection form (drop-down list). I used the code below to get this.
Model Class:
class VerificationAdmin(admin.ModelAdmin):
list_display = ('id','asset_code', 'scan_time','credential','status','operator','location','auth_code','product_details')
list_filter = ('status','operator','location')
ordering = ('-scan_time',)
search_fields = ('asset_code',)
actions = ['set_interval']
class AddScanningIntervalForm(forms.Form):
_selected_action = forms.CharField(widget=forms.MultipleHiddenInput)
period_choice=["4 hrs","6 hrs","8 hrs","10 hrs","12 hrs"]
interval = forms.ChoiceField(choices=[(x, x) for x in period_choice])
@csrf_exempt
def set_interval(self, request, queryset):
print "before action"
form = None
if 'apply' in request.POST:
form = self.AddScanningIntervalForm(request.POST)
print "action taken"
if form.is_valid():
interval = form.cleaned_data['interval']
print interval
count = 0
for vObj in queryset:
print vObj.asset_code,vObj.status,interval
at=AlertTable(asset_code=vObj.asset_code,
status=vObj.status,interval=interval)
at.save()
count += 1
self.message_user(request, "Scanning Policy Successfully added to %s assets %s." %count)
return HttpResponseRedirect(request.get_full_path())
if not form:
form = self.AddScanningIntervalForm(initial={'_selected_action': request.POST.getlist(admin.ACTION_CHECKBOX_NAME)})
return render_to_response('admin/set_alert.html', {'verifications': queryset,'tag_form': form},context_instance=RequestContext(request))
set_interval.short_description = "Add Periodic Scanning Policy"
Add part of the template:
<!DOCTYPE html>
{% extends "admin/base_site.html" %}
{% block content %}
<p>Select tag to apply:</p>
<form action="" method="post">
{{ tag_form }}
{% csrf_token %}
<p>The scanning policy will be applied to:</p>
<ul>{{ verifications|unordered_list }}</ul>
<input type="hidden" name="action" value="add_tag" />
<input type="submit" name="apply" value="Set Interval" />
</form>
{% endblock %}
It works fine up to the intermediate page, but after choosing one of the options for choosing an alert in the form on the intermediate page and clicking on the "Interval" button, I get the result "no action selected"