参考了网上的实现,最终实现的各代码如下:
changepwd.html模板文件:
{% extends "Prism/index.html" %} {% load staticfiles %} {% block title %} 修改密码 {% endblock %} {% block heading %} {% endblock %} {% block content %} <div class="tm-middle"> <div class="uk-container uk-container-center"> <h3 class="tm-article-subtitle uk-margin-top"> 修改密码 </h3> <hr class="uk-grid-divider"> <div class="uk-panel uk-panel-box uk-container-center uk-width-1-3"> <h3 class="uk-panel-title"><i class="uk-icon-user"></i>修改密码</h3> <div class="uk-grid"> <div > {% if changepwd_success %} <div class="uk-alert">密码更改成功,请用新的密码登陆!</div> {% endif%} <form class="uk-form uk-form-horizontal uk-margin-top" action="" method="post"> {% csrf_token %} <div class="uk-form-row"> <div class="uk-form-label"> {{ form.oldpassword.label_tag }} {% for error in form.oldpassword.errors %} <span class="uk-badge uk-badge-danger">{{ error }}</span> {% endfor %} {% if oldpassword_is_wrong %} <span class="uk-badge uk-badge-danger"> 原密码不正确 </span> {% endif %} </div> <div class="uk-form-control"> {{ form.oldpassword }} </div> </div> <div class="uk-form-row"> <div class="uk-form-label"> {{ form.newpassword1.label_tag }} {% for error in form.newpassword1.errors %} <span class="uk-badge uk-badge-danger">{{ error }}</span> {% endfor %} </div> <div class="uk-form-control"> {{ form.newpassword1 }} </div> </div> <div class="uk-form-row"> <div class="uk-form-label"> {{ form.newpassword2.label_tag }} {% for error in form.newpassword2.errors %} <span class="uk-badge uk-badge-danger">{{ error }}</span> {% endfor %} </div> <div class="uk-form-control"> {{ form.newpassword2 }} </div> </div> <div class="uk-form-row"> <input type="submit" value="确认修改" class="uk-button uk-button-primary"> </div> </form> </div> </div> </div> </div> </div> {% endblock %}
Forms.py表单文件:
class ChangepwdForm(forms.Form): oldpassword = forms.CharField( required=True, label=u"原密码", error_messages={'required': u'请输入原密码'}, widget=forms.PasswordInput( attrs={ 'placeholder': u"原密码", 'rows': 1, } ), ) newpassword1 = forms.CharField( required=True, label=u"新密码", error_messages={'required': u'请输入新密码'}, widget=forms.PasswordInput( attrs={ 'placeholder': u"新密码", 'rows': 1, } ), ) newpassword2 = forms.CharField( required=True, label=u"确认密码", error_messages={'required': u'请再次输入新密码'}, widget=forms.PasswordInput( attrs={ 'placeholder': u"确认密码", 'rows': 1, } ), ) 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文件:
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 = 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('changepwd.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, }))
urls.py
url(r'^accounts/changepwd/$', login_required(changepwd), name="changepwd"), url(r'^accounts/changeemail/$', login_required(changeemail), name="changeemail"),
最后就当当当当~~~~~~~~