Django项目实战 - 个人中心修改密码
2017-12-13 本文已影响422人
coderST
- 1 url配置
# 密码修改
url(r'^update/pwd/$', UpdatePwdView.as_view(), name='update_pwd'),
- 2 form表单配置
#重置密码(提交重置密码)
class ModifyPwdForm(forms.Form):
password1 = forms.CharField(required=True, min_length=5)
password2 = forms.CharField(required=True, min_length=5)
- 3 views配置
class UpdatePwdView(View):
"""个人中心修改密码"""
def post(self, request):
modify_form = ModifyPwdForm(request.POST)
if modify_form.is_valid():
#判断两次密码是否一致
pwd1 = request.POST.get('password1', '') #与html中name值一样
pwd2 = request.POST.get('password2', '') #与html中name值一样
if pwd1 != pwd2:
return HttpResponse('{"status":"fail", "msg":"密码不一致"}', content_type="application/json")
# 密码加密保存
user = request.user
user.password = make_password(pwd1)
user.save()
return HttpResponse('{"status":"success", "msg":"修改成功"}', content_type="application/json")
else:
return HttpResponse(json.dumps(modify_form.errors), content_type='application/json')
- 4 html配置
<div class="resetpwdbox dialogbox" id="jsResetDialog">
<h1>修改密码</h1>
<div class="close jsCloseDialog"><img src="{% static 'images/dig_close.png' %}"/></div>
<div class="cont">
<form id="jsResetPwdForm" autocomplete="off">
<div class="box">
<span class="word2" >新 密 码</span>
<input type="password" id="pwd" name="password1" placeholder="6-20位非中文字符"/>
</div>
<div class="box">
<span class="word2" >确定密码</span>
<input type="password" id="repwd" name="password2" placeholder="6-20位非中文字符"/>
</div>
<div class="error btns" id="jsResetPwdTips">123</div>
<div class="button">
<input id="jsResetPwdBtn" type="button" value="提交" />
</div>
{% csrf_token %}
</form>
</div>
- 5 js配置
$(function(){
//个人资料修改密码
$('#jsUserResetPwd').on('click', function(){
Dml.fun.showDialog('#jsResetDialog', '#jsResetPwdTips');
});
$('#jsResetPwdBtn').click(function(){
$.ajax({
cache: false,
type: "POST",
dataType:'json',
url:"/users/update/pwd/",
data:$('#jsResetPwdForm').serialize(),
async: true,
success: function(data) {
if(data.password1){
Dml.fun.showValidateError($("#pwd"), data.password1);
}else if(data.password2){
Dml.fun.showValidateError($("#repwd"), data.password2);
}else if(data.status == "success"){
Dml.fun.showTipsDialog({
title:'提交成功',
h2:'修改密码成功,请重新登录!',
});
Dml.fun.winReload();
}
else if(data.msg){
Dml.fun.showValidateError($("#pwd"), data.msg);
Dml.fun.showValidateError($("#repwd"), data.msg);
}
}
});
});