用户登录逻辑

关于登录这个问题,登录界面同样是用form表单,用户输入用户名和密码,点击登录按钮提交form表单,我们通过我们设置的函数获取用户输入的数据,然后拿用户的用户名到数据库里进行匹配,如果匹配不到值说明用户名不存在,我们就在前端页面告知用户,如果匹配到值了则说明用户名存在,我们可以进一步验证密码,如果密码正确则跳转页面到内容页,如果密码错误则在前端页面告知用户。

def login(request):
    if request.method == "POST" and request.POST:
        name = request.POST.get("name")
        password = request.POST.get("password")
        user = User_one.objects.filter(userName=name)
        if user:
            if user[0].password == getPassword(password):
                return HttpResponseRedirect("/student/page_student_list/1/")
            else:
                key = "密码错误"
        else:
            key = "用户名不存在"
    return render(request,"login.html", locals())

在这里插入图片描述以下是加密函数,用于对密码进行加密

import hashlib      #导入hashlib包
def getPassword(password):
    md5 = hashlib.md5()
    md5.update(password.encode())
    result = md5.hexdigest()
    return result

当然我们后续还要在里面加入cookie,这里暂时没有加入

发布了21 篇原创文章 · 获赞 9 · 访问量 9572

猜你喜欢

转载自blog.csdn.net/Zhang_Chao_1998/article/details/86099727