I am new to django and I didnt know that it included a forms library. I have written a dynamic form in html and javascript that shows checkboxes and if clicked, they display more input fields.
I am wondering if there is an easy way to adapt the code that I have already written to use django’s forms library since it is so much easier and cleaner to access the form’s data with djangos built-in methods.
Also, if i want the form to be dynamic do I need to use formset or does the form library have those capabilities?
Here is the code I would like to convert to use the Forms library:
<script language="javascript">
function ifChecked(id, id2){
var ele = document.getElementById(id);
var ele2 = document.getElementById(id2);
if(ele.checked){
ele2.style.display = "block";
}
else{
ele2.style.display = "none";
}
}
function ifCheckedLine(id, id2){
var ele = document.getElementById(id);
var ele2 = document.getElementById(id2);
if(ele.checked){
ele2.style.display = "inline";
}
else{
ele2.style.display = "none";
}
}
</script>
<body>
<form name="myform" action="resultsget" method = "get" >
<fieldset>
<input type="checkbox" value="total_money" id = "money_check" name="check" onchange="javascript:ifChecked('money_check','money');" /> Filter by Total Money</br>
<div id="money" style="display:none" name="option">
<input type="checkbox" value="more" id="money_condition" onchange="javascript:ifCheckedLine('money_condition', 'money_text');" name="condition"/> <label for="condition" > > </label>
<div id = "money_text" style="display:none" >
<input type="text" id="money_box" name="money_name" value="lower limit" />
</div></br>
<input type="checkbox" value="less" id="money_condition1" onchange="javascript:ifCheckedLine('money_condition1', 'money_text1');" name="condition"/> <label for="condition1"> < </label>
<div id = "money_text1" style="display:none;" >
<input type="text" id="money_box1" name="money_name1" value="upper limit" />
</div>
</div>
</fieldset>
<input type="submit" value="Submit" />
</form>
I would also like to know what I have to put in my views.py file to read the form information
Thanks!!
With django forms you can build a form with fields that you want and using template render it.
Processing of a form will become easy when you build a django form. Refer Working with forms
For dynamic enabling/disabling fields, the code you have written can be used with a change that use ‘id’ that django puts in for form field. I will suggest to use jQuery to set event handlers for the elements.