上一篇我们实现了用户认证系统的登录模块,这一篇实现修改密码模块。

同样地,我们首先得给修改密码创建表单(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)如果原密码错误,提示“原密码错误”的错误信息:

如果所有的信息都填写正确,则修改密码成功,返回主页。

[Django实战] 第5篇 - 用户认证(修改密码)的更多相关文章

  1. [Django实战] 第3篇 - 用户认证(初始配置)

    当大家打开一个网站时,第一步做什么?大部分一定是先登录吧,所以我们就从用户认证开始. 打开用户认证 Django本身已经提供了用户认证模块,使用它可以大大简化用户认证模块的开发,默认情况下,用户认证模 ...

  2. [Django实战] 第4篇 - 用户认证(用户登录)

    今天来实现用户登录模块 首先,我们创建一个表单(forms.py): from django import forms from django.contrib.auth.models import U ...

  3. Django之auth模块(用户认证)

    auth模块简介 auth模块是对登录认证方法的一种封装,之前我们获取用户输入的用户名及密码后需要自己从user表里查询有没有用户名和密码符合的对象, 而有了auth模块之后就可以很轻松的去验证用户的 ...

  4. Django之auth模块(用户认证)登陆组件

    auth模块简介 auth模块是对登录认证方法的一种封装,之前我们获取用户输入的用户名及密码后需要自己从user表里查询有没有用户名和密码符合的对象, 而有了auth模块之后就可以很轻松的去验证用户的 ...

  5. 第十五篇 用户认证auth

    用户认证auth 阅读目录(Content) 用户认证 auth模块 1 .authenticate() 2 .login(HttpRequest, user) 3 .logout(request) ...

  6. Ubuntu Server忘记密码后,单用户模式修改密码进去不了桌面的无奈

    俗话说的好,好记性不如烂笔头.有时候脑子一热,就想不起来之前设置过的密码是什么了.我可怜地忘了我的Ubuntu Server的密码,回忆了n种组合都不行,于是只能进行单用户模式的修改密码了. 以下的操 ...

  7. linux 查看用户上次修改密码的日期【转】

    1.找到以下文件: cat /etc/shadow 第三段字符就是最近一次密码修改的天数,此数字是距离1970年1月1日的天数.   2.用以下命令计算: date -u -d "1970- ...

  8. linux添加用户、修改密码

    1.在root下添加用户用 adduser 命令 # 添加用户 admin [root@flm] #sudo adduser admin 2.添加用户登录密码 # 为用户 admin 修改密码 [ro ...

  9. 记一次CentOS7进单用户模式修改密码的失败经历(faild to load SELinux policy freezing)

    背景:Cent SO7.4root用户密码忘记,根据https://www.linuxidc.com/Linux/2016-08/134034.htm提供的放法修改完密码之后系统启动后一直停留在转圈的 ...

随机推荐

  1. python读取文件内容方法

    1) readline 每次读一行,返回序列 2) readlines 一次全部读出,返回序列 3) numpy 的genfromtxt,返回为np的矩阵格式 import numpy as np f ...

  2. JavaScript下全选反选的Demo程序里实现checkmeonly函数 DOM

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. ESRI Shapefiles (SHP)

    ESRI Shapefiles (SHP) Also known as ESRI ArcView Shapefiles or ESRI Shapefiles. ESRI is the company ...

  4. 辛星和您一起手写CSS气泡

    上文中我公布了一篇手写导航条的博客,那么这一篇博客我将和大家一起手写气泡.那么什么是气泡呢?先给那些刚入门的童鞋一个截图,来更好的认识一下什么是气泡把: 这就是一个简单的气泡啦,那么它主要用来干什么呢 ...

  5. 复合文档的二进制存储格式研究[ole存储结构](word,xls,ppt...)[转]

    复合文档文件格式研究   前 言 复合文档(Compound Document) 是一种不仅包含文本而且包括图形.电子表格数据.声音.视频图象以及其它信息的文档.可以把复合文档想象成一个所有者,它装着 ...

  6. 基于visual Studio2013解决C语言竞赛题之1038数字验证

          题目 解决代码及点评 /********************************************************************** ...

  7. Android 调整屏幕分辩率

    Android 可设置为随着窗口大小调整缩放比例及设定fixed的窗口大小. 对于surface的控制在SurfaceHolder类中进行 而Android 屏幕分辩率中已经有一个类DisplayMe ...

  8. GDI+: Curved Shapes

    原文 http://www.functionx.com/vcsharp2003/gdi/curves.htm Curves   Introduction to Curves   A curve is ...

  9. 基于visual Studio2013解决C语言竞赛题之1060寻找回文数

       题目 解决代码及点评 /* 60. 回文数指左右数字对称的数,如121,2112都是回文数.回文数猜想:取一任意十进制数,将其倒过来,并将这两个数相加, 然后把这个相加的和倒过来再与 ...

  10. 运行tomcat6w.exe ,提示 指定的服务未安装 unable to open the service 'tomcat66'

    错误:运行tomcat6w.exe ,提示 指定的服务未安装 unable to open the service 'tomcat6'(我用的是官网下载的解压版) 解决方法: 打开命令行提示符窗口=& ...