I try to enable placeholder at my SignUp form with 4 fields: phone, email, password1, password2. For first two fields all correct, but for password field it doesn\'t work.
I tested the @Alasdair ´s solution and work for me this way:
from django import forms
from django.contrib import auth
from django.contrib.auth.forms import UserCreationForm
from django.utils.translation import ugettext_lazy as _
class RegistrationForm(UserCreationForm):
first_name = forms.CharField(label=_("First name"), widget=forms.TextInput(attrs={'placeholder': _("First name")}))
email = forms.EmailField(label=_("Email"), widget=forms.TextInput(attrs={'placeholder': _("Email")}))
class Meta(UserCreationForm.Meta):
model = auth.get_user_model()
fields = [
'first_name',
'email',
'password1',
'password2'
]
def __init__(self, *args, **kwargs):
super(InviteRegistrationForm, self).__init__(*args, **kwargs)
self.fields['password1'].widget = forms.PasswordInput(attrs={'placeholder': _("Password")})
self.fields['password2'].widget = forms.PasswordInput(attrs={'placeholder': _("Password")})
The Meta.widgets
option doesn't apply to fields that were declared in the form. See the note in the docs. In this case, password1
and password2
are declare on the UserCreationForm
(they aren't model fields), therefore you can't use them in widgets
.
You could set the widget in the __init__
method instead:
class SignUpForm(UserCreationForm):
class Meta:
model = User
fields = ('phone', 'email', 'is_client', 'is_partner')
widgets = {
'email': EmailInput(attrs={'class': 'form-control', 'placeholder': 'Email adress'}),
'phone': TextInput(attrs={'class': 'form-control', 'placeholder': 'Phone number +79011234567'}),
}
def __init__(self, *args, **kwargs):
super(SignUpForm, self).__init__(*args, **kwargs)
self.fields['password1'].widget = PasswordInput(attrs={'class': 'form-control', 'placeholder': 'Password from numbers and letters of the Latin alphabet'})
self.fields['password2'].widget = PasswordInput(attrs={'class': 'form-control', 'placeholder': 'Password confirmation'})
This code works fine
class SignUpForm(UserCreationForm):
password1 = forms.CharField(max_length=16, widget=forms.PasswordInput(attrs={'class': 'form-control', 'placeholder': 'Password from numbers and letters of the Latin alphabet'}))
password2 = forms.CharField(max_length=16, widget=forms.PasswordInput(attrs={'class': 'form-control', 'placeholder': 'Password confirm'}))
class Meta:
model = User
fields = ('phone', 'email', 'password1', 'password2', 'is_client','is_partner')
widgets = {
'email': EmailInput(attrs={'class': 'form-control', 'placeholder': 'Email adress'}),
'phone': TextInput(attrs={'class': 'form-control', 'placeholder': 'Phone number format +79011234567'}),
}