我有表单字段{{form.item}}将呈现到
<input type="text" name="item" >
如何使用自定义模板标签更改表单字段的名称属性?
我尝试通过将表单发送到模板标签
form.fields['item'].widget.attrs['name'] = 'new_name'
但我没有成功。
我需要更改模板中的名称属性。
更新
模型.py
class A(models.Model):
name = models.CharField(50)
type = models.CharField(50)
class B(models.Model):
field1 = ForeignKeyField(A)
value = IntegerField()
视图.py
def saving_calculation(request):
SavingFormset = modelformset_factory(A, extra=2)
OfferInlineFormSet = inlineformset_factory(
A, B,
extra = 4
)
if request.method == 'POST':
pass
else:
offer_formset = OfferInlineFormSet()
saving_formset = SavingFormset(queryset=SavingCalculation.objects.none())
return render_to_response(
'purchasing/saving_calculation.html',
{
'offer_formset':offer_formset,
'saving_formset':saving_formset,
}
模板
<form action="." method="POST">
{{ offer_formset.management_form }}
{{ saving_formset.management_form }}
{{ saving_formset.prefix }}
<table>
<thead>
<tr>
<th>Business Unit</th>
<th>Category</th>
<th>Buyer</th>
<th>Offer1</th>
<th>Offer2</th>
<th>Offer3</th>
<th>Offer4</th>
</tr>
</thead>
<tbody>
{% for saving in saving_formset.forms %}
<tr>
<td>{{saving.businessunit}}</td>
<td>{{saving.type_of_purchase}}</td>
<td>{{saving.buyer}}</td>
{% for offer in offer_formset.forms %}
<td>{{ offer|set_field_attr:forloop.counter0 }}</td>
</tr>
{% endfor %}
{% endfor %}
</tbody>
</table>
<input type="submit" value="Save" />
</form>
现在在自定义模板标签中,我需要为内联表单集的每个字段分配新名称