Sirion Sirion - 2 years ago 101
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?

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download