【问题标题】:Django Custom ChangeUserForm setting is_active, is_staff, and is_superuser to FalseDjango 自定义 ChangeUserForm 设置 is_active、is_staff 和 is_superuser 为 False
【发布时间】:2023-03-07 08:00:01
【问题描述】:

我有一个自定义 ChangeUserForm(ModelForm on User),允许用户更新他们的帐户信息。

但是,当我保存表单时,user.is_activeuser.is_staffuser.is_superuser 都设置为 错误.

对这里发生的事情有什么想法吗?

forms.py

class UserChangeForm(forms.ModelForm):
    username = forms.RegexField(label="Username", max_length=30, regex=r'^[\w.@+-]+$',
        help_text = "Required. 30 characters or fewer. Letters, digits and @/./+/-/_ only.",
        error_messages = {'invalid': "This value may contain only letters, numbers and @/./+/-/_ characters."})
    first_name = forms.CharField(label="First name", max_length=30)
    last_name = forms.CharField(label="Last name", max_length=30)
    email = forms.EmailField(label="E-mail Address")
    new_password1 = forms.CharField(label="New password", widget=forms.PasswordInput, required=False)
    new_password2 = forms.CharField(label="Confirm new password", widget=forms.PasswordInput, required=False)

    class Meta(auth_forms.UserChangeForm):
        model = User
        exclude = ('password', 'last_login', 'date_joined')

    def clean_new_password2(self):
        password1 = self.cleaned_data.get('new_password1')
        password2 = self.cleaned_data.get('new_password2')

        if password1 != password2:
            raise forms.ValidationError("The two password fields didn't match.")
        else:
            if len(password2) > 0 and len(password2) < 8:
                raise forms.ValidationError("Your password must be a minimum of 8 characters.")
        return password2

    def save(self, commit=True):
        user = super(UserChangeForm, self).save(commit=False)
        if len(self.cleaned_data['new_password2']) > 0:
            user.set_password(self.cleaned_data['new_password2'])
        if commit:
            user.save()
        return user

    class UserProfileForm(forms.ModelForm):
        class Meta:
            model = UserProfile
            exclude = ('user')

views.py

@login_required
def profile(request):
    context = {}
    if request.method == 'POST':
        user_form = UserChangeForm(request.POST, instance = request.user)
        user_profile_form = UserProfileForm(request.POST, instance = request.user.profile)
        if user_form.is_valid() and user_profile_form.is_valid():
            user_form.save()
            user_profile_form.save()

            return render_to_response('accounts_profile_complete.html', context_instance=RequestContext(request))
    else:
        user_form = UserChangeForm(instance = request.user)
        user_profile_form = UserProfileForm(instance = request.user.profile)
    context.update(csrf(request))
    context['user_form'] = user_form
    context['user_profile_form'] = user_profile_form

    return render_to_response('accounts_profile.html', context, context_instance=RequestContext(request))

【问题讨论】:

    标签: python django django-forms django-views


    【解决方案1】:

    只是一个猜测:它们不在 Meta 类的排除列表中,因此它们被设置为 false(像那些使用复选框的布尔字段,未选中时不会显示在 POST 数据中) .表单无法区分设置为 false 的布尔表单字段与页面上不存在的字段之间的区别,因此您需要明确排除它们。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-09
      • 1970-01-01
      • 1970-01-01
      • 2021-11-11
      • 2014-09-30
      相关资源
      最近更新 更多