如何获取for循环生成的表单的值

时间:2014-07-25 09:42:06

标签: python django

请,我正在尝试创建一个税务计算器应用程序,用户输入x个设备,然后将x保存到数据库,然后在同一个HTML页面上显示一个包含x个字段的表单用户填写每个设备的电源

views.py

def input_gadget(request):
if request.user.is_anonymous():
    return HttpResponseRedirect('/')
if request.method == 'POST':
    form = NumberOfGadgets(request.POST)
    user = request.user
    input_form = False
    if form.is_valid():
        gadget = None
        no_of_gadgets = form.cleaned_data['gadgets']
        current_user = request.POST.get('user')
        data = TaxDetails(gadgets=no_of_gadgets, user=user)
        data.save()
        input_form = True
        gadgets = TaxDetails.objects.get(user=user, latest=True)
        fields = range(1, gadgets.gadgets+1)
        if request.method == 'POST':
            for field in fields:
                power_input = request.POST.get(field)
                tax = 'value1 + value2 + value3'
                TaxDetails.objects.filter(user=user, latest=True).update(tax=tax)
                due = TaxDetails.objects.get(latest=True, user=user)
                context = {'due': due, 'form': form, 'input_form': input_form, 'fields': fields, 'user': user}
                return render_to_response('inputdata.html', context, context_instance=RequestContext(request))
        context = {'form': form, 'input_form': input_form, 'fields': fields, 'user': user}
        return render_to_response('inputdata.html', context, context_instance=RequestContext(request))
else:
    form = NumberOfGadgets()
user = request.user
taxdetails = TaxDetails.objects.get(user=user, latest=True)
context = {'form': form, 'taxdetails': taxdetails}
return render_to_response('inputdata.html', context, context_instance=RequestContext(request))

模板

<div id="profile_base">
        <div id="header">
            <h3 style="color: #fff; text-align: center"> Enter your data</h3>
        </div>

        <form action="#" method="post" name="main_form">
            <input type="hidden" name="main_form" value="no_of_gadgets" />
            {% csrf_token %}
                <p>Previous number of gadgets: {{ taxdetails.gadgets }}</p>
                <p>Your previous calculation was on: {{ taxdetails.creation_date }}</p>
                <p>Your previous tax was: {{ taxdetails.tax }}</p>
                <p>
                    {{ form }}
                </p>
            <input type="hidden" name="current_user" value="{{ user }}"/>
            <input type="submit" value="Enter" />
            <a href= '/' > <input type="button" value="Cancel"></a>
        </form>
        <form name="data_form" action="#" method="post">
        <input type="hidden" name="action" value="calcuclate">
            {% csrf_token %}
            {% if input_form %}
                {% for field in fields %}
                    <p><label for="{{ field }}">Enter item {{ field }} power(in WATT)</label> <input type="text" name="{{ field }}"/></p><br/>
                {% endfor %}
                <p id="data_error"></p>
                <input type="submit" value="Calculate" name="Calculate"/>
            {% endif %}
        </form>
    {{ due.tax }}
    </div>

我遇到的问题是获取此生成的x个字段的值而不将这些值保存到数据库中,在某些计算中使用这些值。例如,用户输入5作为设备数量,页面重新加载和生成一个包含5个字段的表单,如何获取每个字段的值以在我的views.py中执行总计,如 value1 + value2 + value3 + value4 + value5 ?感谢

0 个答案:

没有答案