python - Django - create form fields array -
i need have array of form fields in django.
this form class:
class reportnewform(forms.form): monitors_website = site24monitorinfo.many( and(q.state == 0, q.type == 'url'), sort=[('display_name', asc)] ) monitors_choice = [] monitor in monitors_website: monitors_choice.append((monitor['_id'], monitor['display_name'])) website_site24_monitors = forms.choicefield( label='site24x7 monitor', choices=monitors_choice, widget=forms.select( attrs={ 'class': 'form-control', 'id': 'website_site24_monitors[]', 'name': 'website_site24_monitors[]', 'data-parsley-required': "true", 'data-parsley-required-message': "this field required." } ) )
this template
<fieldset> <legend>websites</legend> <div class="input_fields_wrap"> <button class="btn btn-success add_field_button">add more websites</button> <fieldset> <label>website 1</label> <div class="form-group"> <label class="control-label" for="{{ form_report_new.website_site24_monitors.id_for_label }}">{{ form_report_new.website_site24_monitors.label_tag }}</label> {{ form_report_new.website_site24_monitors }} </div> <div class="form-group"> <label class="control-label" for="{{ form_report_new.website_site24_analyzers.id_for_label }}">{{ form_report_new.website_site24_analyzers.label_tag }}</label> {{ form_report_new.website_site24_analyzers }} </div> <div class="form-group"> <label class="control-label" for="{{ form_report_new.website_ssllabs.id_for_label }}">{{ form_report_new.website_ssllabs.label_tag }}</label> {{ form_report_new.website_ssllabs }} </div> </fieldset> </div> </fieldset>
and associated js block
<script type="text/javascript"> $(document).ready(function() { var max_fields = 7; //maximum input boxes allowed var wrapper = $(".input_fields_wrap"); //fields wrapper var add_button = $(".add_field_button"); //add button id var x = 1; //initlal text box count $(add_button).click(function(e){ //on add input button click e.preventdefault(); if(x < max_fields){ //max input box allowed x++; //text box increment $(wrapper).append( '<div id="webserver-' + x + '" class="webserver-' + x + '">' + `<fieldset> <label>website ` + x + `</label> <div class="form-group"> <label class="control-label" for="{{ form_report_new.website_site24_monitors.id_for_label }}">{{ form_report_new.website_site24_monitors.label_tag }}</label> {{ form_report_new.website_site24_monitors }} </div> <div class="form-group"> <label class="control-label" for="{{ form_report_new.website_site24_analyzers.id_for_label }}">{{ form_report_new.website_site24_analyzers.label_tag }}</label> {{ form_report_new.website_site24_analyzers }} </div> <div class="form-group"> <label class="control-label" for="{{ form_report_new.website_ssllabs.id_for_label }}">{{ form_report_new.website_ssllabs.label_tag }}</label> {{ form_report_new.website_ssllabs }} </div> <div class="form-group"> <a href="#" role="button" class="btn btn-danger remove_field" parent-id="webserver-` + x + `">remove</a> </div> </fieldset></div>` ); //add input box } }); $(wrapper).on("click",".remove_field", function(e){ //user click on remove text e.preventdefault(); console.log($(this).attr("parent-id")); $("#" + $(this).attr("parent-id")).remove(); x--; }) }); </script>
basically need have field name square brackets create array of fields (so can use js add them dinamically)
what django stripped out square brackets field name.
is there way have work?
Comments
Post a Comment