简体   繁体   中英

Setting increasing ids in Django ChoiceField form

Is there a way to change id values in Django ChoiceField forms?

For example I'd like my ChoiceField ids to output in the format id="rating#" where # is an incrementing number. I assume you change the widget attributes for id, but how would I get the incrementing number in the id?

Example:

<label for="id_rating_0"><input type="radio" id="rating1" value="1" name="rating" /> </label>

<label for="id_rating_1"><input type="radio" id="rating2" value="2" name="rating" /> </label>

<label for="id_rating_2"><input type="radio" id="rating3" value="3" name="rating" /> </label>

<label for="id_rating_3"><input type="radio" id="rating4" value="4" name="rating" /> </label>

<label for="id_rating_4"><input type="radio" id="rating5" value="5" name="rating" /> </label>

为什么不使用forloop手动呈现选择

<elem id="whatever_{{ forloop.counter }}"/>

If the auto_id form parameter doesn't do what you want, then I'm afraid I don't know what will:

https://docs.djangoproject.com/en/dev/ref/forms/api/#configuring-html-label-tags

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM