[Answered ]-Set name attribute in Django generated select field

0👍

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.

👤Joseph

2👍

I resolved this problem using javascript.

You can add id attribute in your form:

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

And at the end of the template you can put this javascript code:

<!DOCTYPE html>
<html>
<head>
...
</head>

<body>
...
...
<script type="text/javascript">
$(document).ready(function(){ 
document.getElementById('id_player').name = 'to_player';
});
</script>

</body>
</html>

In this way I solved this problem., and works 100% !

Have a nice day !

Leave a comment