0

我有以下视图、模板和表单,我通过这些视图、模板和表单来实现表单集。我打算在单个视图/模板中实现多个表单集,但我的表单集数量是基于用户输入的动态的。如何在此代码中拥有多个动态数量的表单集?我可以用字典元素来做到这一点,即通过创建一个表单集字典吗?

我的看法如下:

def show (request):

 b = request.session["s1"] # count of no of period ids
 c = request.session["s2"] # account number inserted by user
 d = request.session["s3"] # year inserted by customer
 a = account_period.objects.filter(year=d).values('id')
 e = account_period.objects.filter(year=d).values('month')
 f = account_period.objects.filter(id = a).values('year') 
 butt = formset_factory(bu, extra=b)
 if request.method == 'POST'
  formset = butt(request.POST)
  if formset.is_valid():

    z = account_tab.objects.get(account_number=c)
    pr = date.today()
    i=0
    for form in formset.forms:
      x = form.cleaned_data['value']
      y = account_period.objects.get(id=a[i:(i+1)])
      try:
             uip = budget.objects.get(account_no = c,account_period = a[i:(i+1)])
             if uip.budget_amount != x
               uip.budget_amount = x
               uip.save()

      except budget.DoesNotExist:
             w = budget(account_no = z, account_period = y, budget_amount = x, created_by_login = 'me')
             w.save()
      i=i+1
    pass
    return HttpResponse('thanks')

形式是

class bu(forms.Form):
   value = forms.CharField()

模板是

<html>
<head>
<title>BUDGET</title>
</head>
<body>
<p>BUDGET MANAGEMENTS</p>
<p>Your Account Number is : {{ account_number }}.</p> <p>You Chose {{ period }} {{month}} as period<p>   
{% if form.errors %}
<p style="color: red;">
    Please correct the error{{ form.errors|pluralize }} below.
</p>
{% endif %} 
<form action="." method="post">{% csrf_token %}
{{ formset.management_form }}
<table>
{% for form in formset.forms %}
{{ form }}
{% endfor %}
</table>    
<input type="submit" value="Submit">
</form>
</body>
</html>
#

@rohan

我的 GET 方法返回有许多变量必须传递到模板中,所以我尝试通过以下两种方式传递字典 formsetlist(在应用你建议的更改之后)

1)

formset = butt( return render_to_response('budgetfinalform.html', {'account_number': c,'period':d,'month':e,'year':f,'formset': formset},context_instance=RequestContext(request))

2)

ctx ={'formsetlist': formset} 

 formset = butt( return render_to_response('budgetfinalform.html', {'account_number': c,'period':d,'month':e,'year':f,ctx,context_instance=RequestContext(request))

但获得“unboundlocalerror:分配前引用的局部变量'formset'”

4

1 回答 1

0

我会做类似的事情:

def show (request):
  #initial view processing
  # fomset_count taken as parameter or user input
  formsetlist = []
  #create formset list
  for i in range(0, formset_count):
    formsetlist.append(formset_factory(bu, extra=b))
  # other view related code

 # for GET method    
 ctx = { 'formsetlist': formset }
 return render_to_response('your_template_name', ctx,
                          context_instance = RequestContext(request)
                          )

在模板中:

<form action="." method="post">{% csrf_token %}
 {% for formset in formsetlist %}
    {{ formset.management_form }}
    <table>
    {% for form in formset.forms %}
    {{ form }}
    {% endfor %}
    </table>    
{%endfor%}
<input type="submit" value="Submit">
于 2012-07-05T10:02:53.390 回答