上一篇我们实现了用户认证系统的登录模块,这一篇实现修改密码模块。
同样地,我们首先得给修改密码创建表单(forms.py):
class ChangepwdForm(forms.Form):
oldpassword = forms.CharField(
required=True,
label=u"原密码",
error_messages={'required': u'请输入原密码'},
widget=forms.PasswordInput(
attrs={
'placeholder':u"原密码",
}
),
) newpassword1 = forms.CharField(
required=True,
label=u"新密码",
error_messages={'required': u'请输入新密码'},
widget=forms.PasswordInput(
attrs={
'placeholder':u"新密码",
}
),
) newpassword2 = forms.CharField(
required=True,
label=u"确认密码",
error_messages={'required': u'请再次输入新密码'},
widget=forms.PasswordInput(
attrs={
'placeholder':u"确认密码",
}
),
) def clean(self):
if not self.is_valid():
raise forms.ValidationError(u"所有项都为必填项")
elif self.cleaned_data['newpassword1'] <> self.cleaned_data['newpassword2']:
raise forms.ValidationError(u"两次输入的新密码不一样")
else:
cleaned_data = super(ChangepwdForm, self).clean()
return cleaned_data
接着我们在views.py创建一个修改密码的视图:
@login_required
def changepwd(request):
if request.method == 'GET':
form = ChangepwdForm()
return render_to_response('changepwd.html', RequestContext(request, {'form': form,}))
else:
form = ChangepwdForm(request.POST)
if form.is_valid():
username = request.user.username
oldpassword = request.POST.get('oldpassword', '')
user = auth.authenticate(username=username, password=oldpassword)
if user is not None and user.is_active:
newpassword = request.POST.get('newpassword1', '')
user.set_password(newpassword)
user.save()
return render_to_response('index.html', RequestContext(request,{'changepwd_success':True}))
else:
return render_to_response('changepwd.html', RequestContext(request, {'form': form,'oldpassword_is_wrong':True}))
else:
return render_to_response('changepwd.html', RequestContext(request, {'form': form,}))
其中,changepwd.html的定义如下:
<!DOCTYPE html>
{% load bootstrap_toolkit %}
{% load url from future %}
<html lang="en">
<head>
<meta charset="utf-8">
<title>数据库脚本发布系统</title>
<meta name="description" content="">
<meta name="author" content="朱显杰">
{% bootstrap_stylesheet_tag %}
{% bootstrap_stylesheet_tag "responsive" %}
<style type="text/css">
body {
padding-top: 60px;
}
</style>
<!--[if lt IE 9]>
<script src="//html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
{% bootstrap_javascript_tag %}
{% block extra_head %}{% endblock %}
</head> <body> <h2>修改密码</h2> {% if oldpassword_is_wrong %}
<div class="alert alert-error">
<button type="button" class="close" data-dismiss="alert">×</button>
<h4>错误!</h4>原密码不正确
</div>
{% endif %}
<div class="well">
<form class="form-horizontal" action="" method="post">
{% csrf_token %}
{{ form|as_bootstrap:"horizontal" }}
<p class="form-actions">
<input type="submit" value="确认修改" class="btn btn-primary">
</p>
</form>
</div> </body>
</html>
urls.py添加如下:
(r'^accounts/changepwd/$', 'dbrelease_app.views.changepwd'),
最终效果如下:
1)用户登录后,点击”修改密码",显示修改密码的登录框:
2)上述三个域都为必填项,只要有一个为空,就提示“所有项为必填项”的错误信息:
3)如果两次输入的新密码不同,提示“两次输入的新密码不一样”的错误信息:
4)如果原密码错误,提示“原密码错误”的错误信息:
如果所有的信息都填写正确,则修改密码成功,返回主页。