I have a Django 3.0 form
# forms.py
class SignupForm(UserCreationForm):
email = forms.EmailField()
This renders as the HTML element
<input type="text" name="email" required id="id_email">
Is there a way to change the 'name' attribute?
The widgets documentation suggests that either of these might work:
# forms.py
class SignupForm(UserCreationForm):
email = forms.EmailField(
widget = forms.TextInput(
attrs = {'name': 'email_address'}
)
)
or
# forms.py
class SignupForm(UserCreationForm):
email = forms.EmailField()
email.widget.attrs.update({'name': 'email_address'})
but both render with two name
attributes; the first one isn't replaced:
<input type="text" name="email" name="email_address" required id="id_email">
Is there a straightforward method of changing the name
attribute?
I've found a couple of related previous posts, but the questions and answers tend to be old (Django 1.0-era) and more convoluted than this process ought to be. I'm hoping that newer versions have a simpler solution.
question from:
https://stackoverflow.com/questions/65861544/how-do-i-change-the-name-html-attribute-of-a-django-form-field 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…