【问题标题】:Django clean password 'ValidationError' object has no attribute 'get'Django 清理密码“ValidationError”对象没有属性“get”
【发布时间】:2018-05-11 17:48:20
【问题描述】:

我正在使用 django-custom-user 通过电子邮件登录。我有 2 个模型 CustomUser 和 ClientData:

models.py

class CustomUser(AbstractEmailUser):
    first_name = models.CharField(max_length=200, blank=True, null=True, default=None)
    last_name = models.CharField(max_length=200, blank=True, null=True, default=None)
    phone = models.CharField(max_length=20, blank=True, null=True, default=None)

class ClientData(models.Model):
    user = models.OneToOneField(CustomUser)
    company = models.CharField(max_length=200, blank=True, null=True, default=None)
    bank_account = models.CharField(max_length=25, blank=True, null=True, default=None)

我正在尝试用两种型号制作一个注册表单,并且我已经做到了,我还制作了一个干净的密码功能,一切正常,但是当我故意提供 2 个不同的密码时,我得到了:

'ValidationError' object has no attribute 'get'

forms.py

class UserRegistrationForm(forms.ModelForm):
    email = forms.EmailField(required=True, max_length=150)
    first_name = forms.CharField(required=True, max_length=100)
    last_name = forms.CharField(required=True, max_length=100)
    password1 = forms.CharField(required=True, label='Password', max_length=100, widget=forms.PasswordInput())
    password2 = forms.CharField(required=True, label='Confirm Password', max_length=100, widget=forms.PasswordInput())
    phone = forms.CharField(required=True, max_length=20)

    class Meta:
        model = CustomUser
        fields = ['email', 'first_name', 'last_name', 'password1', 'password2', 'phone']

    def clean(self):
        cleaned_data = super(UserRegistrationForm, self).clean()
        password1 = cleaned_data.get('password1')
        password2 = cleaned_data.get('password2')
        if password1 != password2:
            return forms.ValidationError('Password did not match.')
        return cleaned_data

class UserDataRegistrationForm(forms.ModelForm):

    class Meta:
        model = ClientData
        fields = ['company', 'bank_account']

这是我的观点:

views.py

def register(request):
    data = dict()
    if request.method == 'POST':
        user_form = UserRegistrationForm(request.POST)
        data_form = UserDataRegistrationForm(request.POST)
        if user_form.is_valid() * data_form.is_valid():
            cd_user = user_form.cleaned_data
            cd_data = data_form.cleaned_data
            first_name = cd_user['first_name']
            last_name = cd_user['last_name']
            email = cd_user['email']
            password = cd_user['password1']
            phone = cd_user['phone']
            company = cd_data['company']
            bank_account = cd_data['bank_account']
            new_user = CustomUser.objects.create_user(email, password)
            ClientData.objects.create(user=new_user, company=company,  bank_account=bank_account)
            new_user.first_name = first_name
            new_user.last_name = last_name
            new_user.phone = phone
            new_user.company = company
            new_user.bank_account = bank_account
            new_user.save()
    else:
        user_form = UserRegistrationForm()
        data_form = UserDataRegistrationForm()
    data['user_form'] = user_form
    data['data_form'] = data_form
    return render(request, 'registration/register.html', data)

为什么会出现这个错误?

【问题讨论】:

    标签: python django


    【解决方案1】:

    你不应该返回验证错误,你应该提出它们。

    if password1 != password2:
        raise forms.ValidationError('Password did not match.')
    

    【讨论】:

      猜你喜欢
      • 2021-11-12
      • 2017-10-14
      • 2019-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-05
      相关资源
      最近更新 更多