Sirion Sirion - 11 months ago 40
HTML Question

Set name attribute in Django generated select field

I'm trying to write forms using Django but I don't manage to set the name of Select field.

class MyForm(forms.Form):
successor = forms.ChoiceField(widget=forms.Select(
attrs = {'name': 'to_player', 'onclick':'this.form.submit();'}

Gives this output:

<label for="id_successor">Successor:</label>
<select id="id_successor" name="successor" onclick="this.form.submit();"></select>

but should be name="to_player" not name="successor". Why?


I believe the name attribute can't be overwritten using attrs because of the way the widget is rendered. However, if you check out this answer to a similar question, you will find a wrapper function that will allow you to specify your own name attribute.