在数据库中检查用户提交的密码

时间:2015-08-03 21:44:37

标签: python django forms django-authentication

我是Django编程的新手,我遇到了这个问题。 这是我在views.py中的代码:

def login(request):
    if request.method == 'POST':
        email = request.POST['email']
        password = request.POST['password']
    else:
        context = RequestContext(request, {})
        return render(request, 'login.html', context)

如何搜索在html表单中插入电子邮件的用户,并检查密码是否正确?

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

试试这个:

def login(request):
    if request.method == 'POST':
        email = request.POST['email']
        password = request.POST['password']
        # Assuming the fields in your `User` model are labeled the same
        user = authenticate(email=email, password=password)
    else:
        context = RequestContext(request, {})
        return render(request, 'login.html', context)