There's lots of reasons you would want to modify the HTML id. Underscores are ugly when mixed with hyphens!
Use auto_id
.
By default, auto_id is set to the string 'id_%s'.
You could define this when initializing the form:
form = MyForm(auto_id='id-%s')
or change it in the initialization of your form class:
class MyForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
kwargs.update({'auto_id': 'id-%s'})
super().__init__(*args, **kwargs)
Modifying id_for_label(self, id_)
in a custom widget too:
# widgets.py
class MyTextInput(widgets.TextInput):
class Media:
css = {'all': ('myapp/widgets/css/mycustominput.css',)}
js = ('myapp/widgets/js/mycustominput.js',)
def id_for_label(self, id_):
return id_.replace('_', '-')
# forms.py
from .widgets import MyTextInput
class MyForm(forms.ModelForm):
widgets = {
'text': MyTextInput(attrs={'class': 'form-control'})
}