可以在单个视图中使用动态编号的formset并将其传递给单个模板

时间:2012-07-05 09:30:30

标签: python django view formset

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

我的观点如下:

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(在应用你建议的更改之后)跟随2ways

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'”

1 个答案:

答案 0 :(得分: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">