Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用户不会对Django进行身份验证_Django_Django Authentication_Django Users - Fatal编程技术网

用户不会对Django进行身份验证

用户不会对Django进行身份验证,django,django-authentication,django-users,Django,Django Authentication,Django Users,这个问题我已经问过好几次了,我已经做了几天了,但仍然不起作用。用户将不会在Django中进行身份验证。我使用的是基本用户模型。我的用户没有得到认证,表单也没有提交,我不清楚出了什么问题 以下是我的视图.py: def payments(request): if request.method == "POST": form = CustomUserCreationForm(request.POST) if form.is_valid():

这个问题我已经问过好几次了,我已经做了几天了,但仍然不起作用。用户将不会在Django中进行身份验证。我使用的是基本用户模型。我的用户没有得到认证,表单也没有提交,我不清楚出了什么问题

以下是我的视图.py:

 def payments(request):
    if request.method == "POST":
        form = CustomUserCreationForm(request.POST)
        if form.is_valid():
            user = form.save(commit=False)
            user.is_active = False # this is because they need to fill out another form before I deem them active
            user.set_password(form.cleaned_data['password1'])
            user.save() 
            user = authenticate(request, username=form.cleaned_data['username'], password=form.cleaned_data['password1'])
            if user.is_authenticated:
                login(request, user, backend='django.contrib.auth.backends.ModelBackend')
                return redirect('agreements')
            else: 
                raise ValidationError("User could not be authenticated.")            
        else:
            raise ValidationError("Form is not valid. Try Again.")
    else:
        form = CustomUserCreationForm()
  return render(request, 'register.html', {'form': form})
这是我的forms.py:

class CustomUserCreationForm(forms.ModelForm):
    username = forms.CharField(label='Username', widget=forms.TextInput(attrs={'class': "form-control"}))
    password1 = forms.CharField(label='Password', widget=forms.PasswordInput(attrs={'class': "form-control"}))
    password2 = forms.CharField(label='Password confirmation', widget=forms.PasswordInput(attrs={'class': "form-control"}))
    first_name = forms.CharField(label='First Name', widget=forms.TextInput(attrs={'class': "form-control"}))
    last_name = forms.CharField(label='Last Name', widget=forms.TextInput(attrs={'class': "form-control"}))
    email = forms.CharField(label= 'Email', widget=forms.EmailInput(attrs={'class': "form-control"}))

    class Meta:
        model = User
        fields = ['first_name', 'last_name', 'email', 'username']

    def clean_password(self):
        password1 = self.cleaned_data.get('password1')
        password2 = self.cleaned_data.get('password2')
        if password1 and password2 and password1 != password2:
            raise forms.ValidationError("Passwords do not match")
        return password2


    def save(self, commit=True):
        user = super(CustomUserCreationForm, self).save(commit=False)
        user.username = self.cleaned_data['username']
        user.first_name = self.cleaned_data['first_name']
        user.last_name = self.cleaned_data['last_name']
        user.email = self.cleaned_data['email']
        user.set_password(self.cleaned_data['password1'])

        if commit:
            user.save()
        return user

我想这是因为这个
user.is_active=False
如果这个设置为
True
,只有当任何用户进行身份验证并试图登录到Django时才被允许,在文档中检查一下,如果你不想看到它所说的默认行为,他们还有另一个选项

如果要允许非活动用户登录,可以使用AllowlUsersModelBackend或AllowlUsersRemoteUserBackend


使user.is_处于活动状态=真