如何在Django中删除EmailField上的占位符?

时间:2015-11-11 12:34:12

标签: django forms placeholder django-allauth

我正在使用django-allauth并创建了一个继承自SignupForm的自定义注册表单。如何从电子邮件字段中删除默认占位符?我不想要一个占位符。

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()

2 个答案:

答案 0 :(得分:4)

placeholder设置为in the code作为小部件attrs词典中的键。

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

因此,您只需在表单的__init__方法中删除该密钥。

def __init__(self, *args, **kwargs):
    super(UserSignUpForm, self).__init__(*args, **kwargs)
    del self.fields['email'].widget.attrs['placeholder']

答案 1 :(得分:0)

您可以使用jquery as -

从渲染的html中删除占位符属性



$(function(){
    $("#id_login").removeAttr('placeholder');
    $("#id_password").removeAttr('placeholder');
});