Django password fields placeholder

前端 未结 3 2001
不知归路
不知归路 2020-12-11 10:51

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.

相关标签:
3条回答
  • 2020-12-11 11:05

    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")})
    
    0 讨论(0)
  • 2020-12-11 11:13

    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'})
    
    0 讨论(0)
  • 2020-12-11 11:21

    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'}),
    
    
            }
    
    0 讨论(0)
提交回复
热议问题