Javed
Javed

Reputation: 6239

how to assign unique html id to a form field for every django form instance

My form is

class AnswerForm(ModelForm):
    class Meta:
      model=Answers
      fields=['answer']
      widgets ={'answer': TinyMCE(attrs={'cols': 80, 'rows': 30},  mce_attrs={'mode':"textareas",}),}

and in my view I am creating an instance of the form answerForm=AnswerForm(). In the template I have many instances of the answerForm inside a for loop

<form action="/q/answer/" method="post" style="display:none;" class="answer_form" >
{% csrf_token %}
{# {{ context.answerForm.as_p }} #}
{# <textarea id="{{ forloop.counter }}">{{ context.answerForm.answer }}</textarea> #}
{{ context.answerForm.answer }}

<input type="hidden" name="q_id_a" id="q_id_a" value="{{ result.object.id }}">
<input type="submit" value="Submit" />

my question:

Upvotes: 2

Views: 926

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599628

If you have multiple instances of a form on a page, you should be using a formset, which will take care of creating unique IDs and field names for you.

Upvotes: 4

Related Questions