Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django admin site action not working after adding an intermediate Form

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 choice form (drop-down selection). I used below code to get this.

The 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 the template part:

<!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's working fine up to Intermediate page, but after selecting one of the alert choice in form at intermediate page and clicking on set Interval button I am getting result "no action selected"

like image 632
PK10 Avatar asked Apr 19 '14 08:04

PK10


1 Answers

This question is a bit old but for any future reference; make sure the 'action' input property in the intermediate form matches the admin function name. In this case 'set_interval' instead of 'add_tag'.

Also the POST context must contain '_selected_action' values which should match the selected items pks.

like image 170
Erik Svedin Avatar answered Oct 23 '22 06:10

Erik Svedin