三行代码  ›  专栏  ›  技术社区  ›  zayant21

使用表单和Django进行用户登录身份验证

  •  0
  • zayant21  · 技术社区  · 2 月前

    我的login.views如下所示

    def login(request):
    if request.method == "POST":
        form = Studentlogin(request.POST)
        if form.is_valid():
            email = form.cleaned_data.get('email')
            password = form.cleaned_data.get('password')
            user = User.objects.create_user(email, password)
            try:
                studentemail = students.objects.get(email=email)
                studentpass = students.objects.get(password=password)
                return render (request, 'subscrap/main.html', {'student': studentemail })
            except:
                messages.success(request, 'Error, either Email or Password is not correct')
                pass
    else:
        form = Studentlogin()
    return render(request, 'subscrap/login.html', {'form': form})
    

    我的学生模型如下所示:

    class students(models.Model):
    fname = models.CharField(max_length=50)
    lname = models.CharField(max_length=50)
    password = models.CharField(max_length = 50 , null = True)
    passwordrepeat = models.CharField(max_length = 50, null = True)
    email = models.EmailField(max_length=150)
    class Meta:
        db_table = "students"
    

    我的表格文件:

    class StudentForm(forms.ModelForm):
        class Meta:
            model = students
            fields = "__all__"
    
    class Studentlogin(forms.Form):
        email = forms.EmailField(max_length=150)
        password = forms.CharField(max_length = 50, widget=forms.PasswordInput)
    
    1 回复  |  直到 2 月前
        1
  •  0
  •   RedWheelbarrow    2 月前

    您只需进行一次查询,即可获得具有给定电子邮件和密码的学生:

    def login(request):
        if request.method == "POST":
            form = Studentlogin(request.POST)
            if form.is_valid():
                email = form.cleaned_data.get('email')
                password = form.cleaned_data.get('password')
                user = User.objects.create_user(email, password)
                try:
                   student = students.objects.get(email=email, password=password)
                   return render (request, 'subscrap/main.html', {'student': student})
                except:
                   messages.success(request, 'Error, either Email or Password is not correct')
            else:
               form = Studentlogin()
            return render(request, 'subscrap/login.html', {'form': form})
    
        2
  •  0
  •   phil    2 月前

    您的查询设置在学生模型上 students.objects.get(email=email) student . 笔记 .get() 返回一个 DoesNotExist 如果没有匹配的记录,则出错。

    根据你所拥有的,你可以做(不理想)

    try
        student = students.objects.get(email=email)
        if student.password == password:
            return render (request, 'subscrap/main.html', {'student': email})  # (or student.email)
        else:
            raise Exception()
    except:
        messages.success(request, 'Error, either Email or Password is not correct')
    

    email password . 尽管如此,查询集仍然返回一个学生,因此如果您只想在渲染中返回电子邮件,则需要执行以下操作

    try:
        student = students.objects.get(email=email, password=password)
        return render (request, 'subscrap/main.html', {'student': email})  # (or student.email)
    except:
        messages.success(request, 'Error, either Email or Password is not correct')
    

    阅读更多关于QuerySet的信息 get 方法 here