Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I remove a placeholder on an EmailField in Django?

I am using django-allauth and created a custom signup form that inherits from SignupForm. How do I remove the default placeholder from the email field? I don't want a placeholder.

from allauth.account.forms import SignupForm

class UserSignUpForm(SignupForm):
    first_name = forms.CharField(max_length=155)
    last_name = forms.CharField(max_length=155)
    mobile = forms.CharField(max_length=20)

    def __init__(self, *args, **kwargs):
        super(UserSignUpForm, self).__init__(*args, **kwargs)

        del self.fields['password1']
        del self.fields['password2']

        self.fields['first_name'].widget.attrs.update({
            'class': 'required form-control',
        })
        self.fields['last_name'].widget.attrs.update({
            'class': 'required form-control',
        })
        self.fields['email'].widget.attrs.update({
            'class': 'required email form-control',
        })
        self.fields['mobile'].widget.attrs.update({
            'class': 'required form-control',
        })

    def custom_signup(self, request, user):
        user.first_name = self.cleaned_data['first_name']
        user.last_name = self.cleaned_data['last_name']
        user.mobile_number = self.cleaned_data['mobile']
        user.save()
like image 200
Jamneck Avatar asked Jan 08 '23 04:01

Jamneck


1 Answers

The placeholder is set in the code as a key in in the widget's attrs dict.

email = forms.EmailField(widget=forms.TextInput(
    attrs={'type': 'email',
           'placeholder': _('E-mail address')}))

So you can simply remove that key in the form's __init__ method.

def __init__(self, *args, **kwargs):
    super(UserSignUpForm, self).__init__(*args, **kwargs)
    del self.fields['email'].widget.attrs['placeholder']
like image 130
Alasdair Avatar answered Jan 09 '23 18:01

Alasdair