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

Posted 上海-悠悠

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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修改密码的主要内容,如果未能解决你的问题,请参考以下文章

python测试开发django-162.ajax 提交表单,防重复提交(beforeSend)

python测试开发django-198.bootstrap-formvalidation校验成功发ajax请求

python测试开发django-183.bootstrap-formvalidation重置校验的方法

python测试开发django-178.ajax实现form表单文件上传

python自动开发之第二十一天

ASP.NET MVC 网站开发总结——Ajax异步提交表单之检查验证码