khadija.EL
khadija.EL

Reputation: 307

Getting the number of formset's id in Django

In forms.py I have created a formset using formset_factory with extra=6

  class Associe(forms.Form):
      nom= forms.CharField(max_length=40)
  AssocieFormSet=formset_factory(Associe,extra=6)

in my page.html I wrote a loop to write my forms 6 times, but I nead the number in the id, I know Django will give form-0-nom for the first element, and id_form-1-nom for the seconde element and so on, and I know how to get the whole id of the element.

But how can I get only the number of each element's id?

Upvotes: 0

Views: 1371

Answers (1)

HoneyNutIchiros
HoneyNutIchiros

Reputation: 551

I think an easy way of getting the form number would be by using the template tags forloop.counter0 or forloop.counter. So with forloop.counter0, form-0-nom will have the number 0 within that iteration and you can do what you want with that number.

Django Built In Template Tags

Upvotes: 1

Related Questions