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),
]

實現效果如下


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM