Django密码字段占位符

时间:2018-03-21 17:40:05

标签: django

我尝试在SignUp表单中启用占位符,其中包含4个字段:phone,email,password1,password2。前两个字段都正确,但对于密码字段,它不起作用。

forms.py

from django import forms
from django.contrib.auth.forms import UserCreationForm
from customuser.models import User
from django.forms import TextInput,EmailInput,PasswordInput

class SignUpForm(UserCreationForm):    


    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  +79011234567'}),
            'password1': PasswordInput(attrs={'class': 'form-control', 'placeholder': 'Password from numbers and letters of the Latin alphabet'}),
            'password2': PasswordInput(attrs={'class': 'form-control', 'placeholder': '
Password confirmation'}),

        }

3 个答案:

答案 0 :(得分:1)

Meta.widgets选项不适用于在表单中声明的​​字段。请参阅note in the docs。在这种情况下,password1password2会在UserCreationForm上声明(它们不是模型字段),因此您无法在widgets中使用它们。

您可以使用__init__方法设置窗口小部件:

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'})

答案 1 :(得分:0)

此代码可以正常使用

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'}),


        }

答案 2 :(得分:0)

我测试了@Alasdair的解决方案,并以此方式为我工作:

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")})