Ninga
Ninga

Reputation: 699

Django dynamic form fields - onchanged

I'm unsure how to get the onchange attribute working propperly on the 'position_type' select widget below. I want it to 'post' the form when a field selection is changed. Presently with this code, the browser indicates there is an 'error on page' when 'onchange' fires which I assume means its not recognizing the resultant 'this.form.submit();' command. (Am I taking the other examples I've found too literally by using 'this.form.submit();' as is or should I substitute in my form name somehow?)

class tranny_form(forms.Form):

   def __init__(self, *args, **kwargs):
           super(tranny_form, self).__init__(*args, **kwargs)
           dict=args[0]  #this extracts the settings passed to 'tranny_form' as a dict

           position_list = [('GGA','GGA'),('DD','DD'),('UTM','UTM'),]
           self.fields['position_type'] = forms.CharField(widget=forms.Select(choices=position_list, attrs={'onchange':'this.form.submit();'}))

           if dict.get('position_type') in ['DD','UTM',]: 
               self.fields['easting_longitude_column'] = forms.IntegerField(max_value=100, min_value=1)
               self.fields['northing_latitude_column'] = forms.IntegerField(max_value=100, min_value=1)

my template code....

<form action="" enctype="multipart/form-data" method="post"> {% csrf_token %}
    <table>
        {{ form.as_table }}
    </table> </br>
    <input type="submit" value="Submit"></br>
</form>

Upvotes: 1

Views: 402

Answers (1)

Ninga
Ninga

Reputation: 699

OK the only problem with the code is that the examples on the web I was copying used the command this.form.submit(); when the correct command is this.form.submit(). Hope this saves some others. My dynamic fields work perfectly now and come and go as required by the selections.

Upvotes: 1

Related Questions