为什么Django中的AuthenticationForm()无效?

qvtsj1bj  于 2023-01-27  发布在  Go
关注(0)|答案(3)|浏览(80)

所以我一直尝试在Django中创建一个功能,让用户登录到他们的帐户。但它 * 只对超级用户有效 *。当我尝试从其他帐户登录时,它不断重置密码,并给出错误消息“请输入正确的用户名和密码”。然而,用户名和密码都是正确的。
来自 request.POST 的数据作为 QuerySet(键-'csrfmiddlewaretoken'、'username'、'password')传入。当我将request.POST放入**AuthenticationForm()**时,它没有经过'ifform.is_valid():'部分。
我该怎么做才能让这个工作?拜托,有人能帮帮我吗?先谢了。
下面是代码:

def log_in(request):
    form=AuthenticationForm()
    if request.method=="POST":
        form=AuthenticationForm(request, data=request.POST)
        if form.is_valid():
            username=form.cleaned_data.get('username')
            password=form.cleaned_data.get('password')
            user=authenticate(username=username,password=password)
            if user is not None:
                login(request,user)
                messages.info(request,'You have successfully logged in!')
                return redirect('news')
            else:
                messages.error(request,'Invalid data!') 
        else:
            messages.error(request,'Invalid data!')           
    return render(request,'post/login.html',{'form':form})



def register_user(request):
    form=UserCreateForm
    if request.method=='POST':
        form=UserCreateForm(request.POST)
        if form.is_valid():
            user=form.save()
            login(request,user)
            messages.success(request,'Registration was successfull')
            return redirect('news')
        messages.error(request,'Try again!')   
    return render(request,'post/register.html',{'form':form})



class UserCreateForm(UserCreationForm):
    email=forms.EmailField(required=True)

    class Meta:
        model=User
        fields=['username','email','password1','password2']

    def save(self,commit=True):
        user=super(UserCreationForm, self).save(commit=False)
        user.email=self.cleaned_data.get('email')
        if commit:
            user.save()
        return user
lkaoscv7

lkaoscv71#

使用下面的代码(django 4.0.6),我可以让用户登录:

def log_in(request):
    form = AuthenticationForm()
    if request.method == "POST":
        form = AuthenticationForm(None, data=request.POST)
        if form.is_valid():
            form.clean()
            user = form.get_user()
            if user is not None:
                login(request, user)
            else:
                print("nay")
        else:
            print("ney")
    return render(request, 'login.html', {'form': form})

您登录方法应如下所示:

def log_in(request):
    form = AuthenticationForm()
    if request.method == "POST":
        form = AuthenticationForm(None, data=request.POST)
        if form.is_valid():
            form.clean()
            user = form.get_user()
            if user is not None:
                login(request,user)
                messages.info(request,'You have successfully logged in!')
                return redirect('news')
            else:
                messages.error(request,'Invalid data!') 
        else:
            messages.error(request,'Invalid data!')           
    return render(request,'post/login.html',{'form':form})

AuthenticationForm已经在它的清理步骤中执行了authenticate-所以你不必这样做。只需调用clean,然后调用get_user函数
PS:记住,你的用户必须是活跃的-〉user.is_active必须是True

dsekswqp

dsekswqp2#

在某些情况下,类名的相似性可能是造成这种情况的原因,这会让我们感到困惑。这就是继承过程中发生错误的地方。您应该编写UserCreateForm类,这样问题就解决了。😎😎😎

def save(self, commit=True):
    user=super(UserCreateForm, self).save(commit=False)
    user.email=self.cleaned_data.get('email')
    if commit:
        user.save()
    return user
5tmbdcev

5tmbdcev3#

尝试从AuthenticationForm(request, data=request.POST)中删除requestAuthenticationForm()函数不需要两个参数

相关问题