python测试开发django-27.表单提交之post修改密码

前言

跟账号相关的功能一般是注册,登录,修改密码,密码找回功能,前面实现了登录和注册功能,本篇讲下修改密码功能实现

修改密码html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>注册页面</title>
</head>
<body>
<h1>新用户注册!</h1>
<form action="" method="post">
    {% csrf_token %}
      <p>
        用户名:<input type="text" id="id_username" name="username", required="required">*
      </p>
      <p>
         密码:<input type="password" id="id_psw" name="password", required="required">*
    </p>
   <p>
         新密码:<input type="password" id="id_new" name="new", required="required">*
    </p>
    <p>{{ msg }}</p>
    <p>
          <input type="submit" value="确定">
    </p>


</form>

</body>
</html>
views和urls
from django.shortcuts import render
from django.contrib.auth.hashers import make_password, check_password

# Create your views here.

def reset_psw(request):
    '''修改密码'''
    res = ""
    if request.method == "GET":
        return render(request, 'reset_psw.html', {'msg': res})

    if request.method == "POST":
        username = request.POST.get('username')
        psw = request.POST.get('password')
        new_psw = request.POST.get('new')

        if psw == new_psw:
            res = "新密码和旧密码不能重复"
            return render(request, 'reset_psw.html', {'msg': res})
        else:
            # 先查询数据库是否有此用户名
            user_lst = User.objects.filter(user_name=username)
            if not user_lst:
                # 如果没这个用户
                res = "用户未注册:%s" % username
                return render(request, 'reset_psw.html', {'msg': res})

            else:
                # 如果注册过,判断密码对不对
                ret = User.objects.filter(user_name=username).first()
                # 校验密码
                is_psw_true = check_password(psw, ret.psw)
                if is_psw_true:
                    user = User()
                    user.psw = make_password(new_psw)
                    user.save()
                    res = "密码修改成功!"
                else:
                    res = "密码错误!"
                return render(request, 'reset_psw.html', {'msg': res})


urls.py访问地址

from django.conf.urls import url
from hello import views

urlpatterns = [
    # 新增用户
    url(r'^register/', views.register),
    url(r'^login/', views.login),
    url(r'^reset/', views.reset_psw),
]

实现效果如下

python测试开发django-27.表单提交之post修改密码

上一篇:Git撤销add、commit、merge


下一篇:Git把文件从暂存区移除