如何在Django表单中操作form.as_p?

时间:2017-08-15 11:43:11

标签: javascript jquery python django forms

我想一次提交两份表格。似乎我必须使用{{form.as_p}}。但现在我面临两个问题。 1.我如何获得{{form.as_p}}的id。在每个表格中我都有输入字段。如何编辑这些字段的宽度?谢谢!

<form  id = "form" action="." method="POST">{% csrf_token %}    

    {{form1.as_p}}  

<div >
    <input id = "geoButton" type="button" class = "btn" value="{{geoFormButtonValue}}" onclick = "formHandler()" />
</div>

<hr>

    {{form2.as_p}}


<div >
    <input id = "totalRiskButton" type="button" class = "btn" value="{{geoFormButtonValue}}" onclick = "formHandler2()" />
</div>

</form>

    <div>
    <input type="button" class = "btn" value="Run" onclick="SubmitForm()" style="float:right"/>
    </div>

原始代码:我原本分别有两个表格。但是,当我提交表单时,我无法通过“.cleaned_data”方法获取数据。更具体地说,我只能从form2获取数据。

的HTML

<form  id = "form1" action="." method="POST">{% csrf_token %}   

<div class=".col1" style="float:left;margin-left:1%">           

    {{ form.region }}

    {{ form.operator }}
</div>

<div id = "form1_value" class="col-xs-2" >
    {{ form.value }}
</div>

</form>

<div >
    <input id = "geoButton" type="button" class = "btn" value="{{geoFormButtonValue}}" onclick = "formHandler()" />
</div>


<hr>

<h1 style="font-size:15px;color:#337AC7;font-weight:bold">Total Risk Constraint</h1>            


<form  id = "form2" action="." method="POST">{% csrf_token %}   
<div class=".col1" style="float:left;margin-left:1%">           

    {{ form2.operator }}

</div>

<div id = "form2_value" class="col-xs-2" >
    {{ form2.totalRiskValue }}
</div>
</form> 

<div >
    <input id = "totalRiskButton" type="button" class = "btn" value="{{geoFormButtonValue}}" onclick = "formHandler2()" />
</div>

<hr >



<div>
<input type="button" class = "btn" value="Run" onclick="SubmitForm1()" style="float:right"/>
</div>

views.py

 if request.method == 'POST':

    constraintform = constraintListForm1(request.POST)         
    if constraintform.is_valid():         
        data = constraintform.cleaned_data
        print(data)    
        region = data['region']
        operator = data['operator']
        value = data['value']                        
        constraint = [region, operator, value]

    constraintform2 = constraintListForm2(request.POST)
    if constraintform2.is_valid():
        data = constraintform2.cleaned_data
        operator = data['operator']
        totalRiskValue = data['totalRiskValue']
        totalRiskConstraint = [operator, totalRiskValue]

0 个答案:

没有答案