Note that there are some explanatory texts on larger screens.

plurals
  1. POChange the name attribute of form field in django template using
    primarykey
    data
    text
    <p>I have form field <strong>{{form.item}}</strong> which will render to </p> <pre><code> &lt;input type="text" name="item" &gt; </code></pre> <p>How can i change the name attribute of the form field using custom template tags?</p> <p>I tried by sending the form to template tag where</p> <pre><code> form.fields['item'].widget.attrs['name'] = 'new_name' </code></pre> <p>But I'm not getting success.</p> <p>I need to change the name attribute in template.</p> <p><strong>UPDATE</strong></p> <p><strong>models.py</strong></p> <pre><code>class A(models.Model): name = models.CharField(50) type = models.CharField(50) class B(models.Model): field1 = ForeignKeyField(A) value = IntegerField() </code></pre> <p><strong>views.py</strong></p> <pre><code> 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, } </code></pre> <p><strong>template</strong></p> <pre><code> &lt;form action="." method="POST"&gt; {{ offer_formset.management_form }} {{ saving_formset.management_form }} {{ saving_formset.prefix }} &lt;table&gt; &lt;thead&gt; &lt;tr&gt; &lt;th&gt;Business Unit&lt;/th&gt; &lt;th&gt;Category&lt;/th&gt; &lt;th&gt;Buyer&lt;/th&gt; &lt;th&gt;Offer1&lt;/th&gt; &lt;th&gt;Offer2&lt;/th&gt; &lt;th&gt;Offer3&lt;/th&gt; &lt;th&gt;Offer4&lt;/th&gt; &lt;/tr&gt; &lt;/thead&gt; &lt;tbody&gt; {% for saving in saving_formset.forms %} &lt;tr&gt; &lt;td&gt;{{saving.businessunit}}&lt;/td&gt; &lt;td&gt;{{saving.type_of_purchase}}&lt;/td&gt; &lt;td&gt;{{saving.buyer}}&lt;/td&gt; {% for offer in offer_formset.forms %} &lt;td&gt;{{ offer|set_field_attr:forloop.counter0 }}&lt;/td&gt; &lt;/tr&gt; {% endfor %} {% endfor %} &lt;/tbody&gt; &lt;/table&gt; &lt;input type="submit" value="Save" /&gt; &lt;/form&gt; </code></pre> <p>Now in custom template tag i need to assign new name for each field of inline formset </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload