eroy4u
eroy4u

Reputation: 179

Django: How to avoid duplicated html id for showing field twice in the same form?

I want to generate the same field twice in the same form, which is useful when there are condition to show 1st textInput or 2nd textInput: Django will generate:

<!--same form -->
<input type="radio" value="good_price"/> <!-- show good price if this is checked -->
<input type="radio" value="bad_price"/> <!-- show bad price if this is checked -->

<!--1st -->
Good price <input id="id_name"..../>
<input id="options_with_good_price"/>

<!--2nd -->
Bad Price <input id="id_name".../>
<input id="options_with_bad_price"/>

So there are duplicated html id, how to avoid it? (I can survive with no id generated for these two boxes with duplicated id, but not all the others)

Upvotes: 4

Views: 802

Answers (1)

Guilherme David da Costa
Guilherme David da Costa

Reputation: 2368

If you are using

{{ form.element }}

You can do the following:

{{ form.element|attr:"id:another_name" }}


Sorry didn't realized the template filter i used: http://djangosnippets.org/snippets/729/

I just changed = by :

Upvotes: 1

Related Questions