python測試開發django(25)--表單提交之post修改密碼
阿新 • • 發佈:2020-12-10
前言
跟賬號相關的功能一般是註冊,登入,修改密碼,密碼找回功能,前面實現了登入和註冊功能,本篇講下修改密碼功能實現
修改密碼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" name="username",required="required">* </p> <p> 密碼:<input type="password" name="password",required="required">* </p> <p> 新密碼:<input type="password" name="new",required="required">* </p> <p> {{msg}} </p> <p> <input type="submit" value="確定"> </p> </form> </body> </html>
views和urls
#coding:utf-8 from django.shortcuts import render from django.http import HttpResponse from tb.models import Users from django.contrib.auth.hashers import make_password,check_password def reset_pwd(request): rs="" if request.method=="GET": return render(request,'chpwd.html',{'msg':rs}) if request.method=="POST": username=request.POST.get('username') pwd=request.POST.get("password") new_pwd=request.POST.get("new") if pwd==new_pwd: rs="新密碼和舊密碼不能重複" return render(request,'chpwd.html',{'msg':rs}) else: #先查詢資料庫是否有此使用者名稱 user_lst=Users.objects.filter(user_name=username).first() if not user_lst: #沒這個使用者 rs="{}使用者未註冊".format(username) return render(request,'chpwd.html',{'msg':rs}) else: #如果註冊過,判斷密碼對不對 is_pwd=check_password(pwd,user_lst.pwd) if is_pwd: user=Users.objects.get(user_name=username) user.pwd=make_password(new_pwd) user.save() rs="密碼修改成功!" else: rs="密碼錯誤!" return render(request,'chpwd.html',{'msg':rs})
urls.py訪問地址
# coding:utf-8 from django.conf.urls import url from tb import views urlpatterns = [ # 新增使用者 url(r'^register/', views.register), url(r'^login/', views.login), url('^chpwd/',views.reset_pwd), ]
實現效果如下