I have the following forms.py with radiobutton and other fields in it. Based on the selection of radio button, I should enable or disable other fields in the form.
Forms.py
class SampleForm(forms.ModelForm):
option = forms.TypedChoiceField(choices=BoolChoices, widget=forms.RadioSelect(renderer=HorizontalRadioRenderer,attrs={'onchange':'check_status()'}), coerce=int,)
otherFields = forms.IntegerField(
widget=forms.TextInput(attrs={'size': 7, 'disabled':True}))
.....
.....
class Meta:
model = Sample
class Media:
js = ('checkoption.js'),
My question is should I use templates (extending admin/base_site.html) or just add templates dir under myapp/templates/myapp/template.html
In such cases if i'm using the following template.html and views.py
<form action="{% url myapp.views.check_option %}" method="POST">{% csrf_token %}
{{ form_as_p }}
<input id="submit" type="button" value="Click" />
def check_option(request):
if request.method == 'GET':
form = SsmpleForm()
else:
form = SampleForm(request.POST)
if form.is_valid():
opt = form.cleaned_data['option']
if opt == 1:
form.fields['otherFields'].widget.attrs['enabled'] = True
return render_to_response('verinc/template.html', {'form' : form,},context_instance=RequestContext(request))
Now how to map radio selection to this view? Is there any other solution to design this task in django.
Attempted to try with Ajax
$(document).ready(function(){
function check_status(){
$.get("{% url myapp.views.check_option %}", function(data){
console.log(data);
}
}
})(django.jQuery);