[Answered ]-Is there way to add style to username field โ€“ django forms

1๐Ÿ‘

โœ…

You are overriding the constructor of the Meta class. Not that of the Form, you thus should implement this at:

class UserForm(forms.ModelForm):
    password = forms.CharField(required=True,label=' pass',widget=forms.PasswordInput(attrs={'class': 'form-control','placeholder':' pass ','style': 'font-size:24px;text-align: center;'}))
    first_name = forms.CharField(required=True,label=' first name ',widget=forms.TextInput(attrs={'class': 'form-control','placeholder':' first name ','style': 'font-size:24px;text-align: center;'}) )
    last_name= forms.CharField(required=True,label=' 2nd name ',widget=forms.TextInput(attrs={'class': 'form-control','placeholder':'  2nd name ','style': 'font-size:24px;text-align: center;'}) )
    email= forms.EmailField(required=True,label=' email ',widget=forms.EmailInput(attrs={'class': 'form-control','placeholder':'  email  ... youremail@email.com','style': 'font-size:24px;text-align: center;'}) )

    # constructor of the UserForm, not Meta
    def __init__(self, *args, **kwargs):
        super().__init__(*args,**kwargs)
        self.fields['username'].widget.attrs.update({'class':'form-control','placeholder':'Username','style': 'font-size:24px;text-align: center;'})

    class Meta(UserCreationForm.Meta):
        model = User
        fields = UserCreationForm.Meta.fields + ('username','first_name','last_name','email','password')

Leave a comment