【问题标题】:Django password fields placeholderDjango 密码字段占位符
【发布时间】:2018-08-30 23:27:31
【问题描述】:

我尝试在我的注册表单中使用 4 个字段启用占位符:电话、电子邮件、密码 1、密码 2。前两个字段都正确,但密码字段不起作用。

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

        }

【问题讨论】:

    标签: django


    【解决方案1】:

    我测试了@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")})
    

    【讨论】:

      【解决方案2】:

      Meta.widgets 选项不适用于在表单中声明的​​字段。请参阅note in the docs。在这种情况下,password1password2UserCreationForm 上声明(它们不是模型字段),因此您不能在 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'})
      

      【讨论】:

      • 嗯,我认为这是我需要的,但由于某种原因它对我不起作用(
      • 我没有测试过代码,但我无法发现它的问题。希望你能弄明白。
      【解决方案3】:

      这段代码运行良好

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

      【讨论】:

        猜你喜欢
        • 2019-08-17
        • 2021-05-02
        • 2013-09-25
        • 1970-01-01
        • 2011-08-28
        • 2014-03-28
        • 2012-09-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多