[Django实战] 第5篇 - 用户认证(修改密码)
上一篇我们实现了用户认证系统的登录模块,这一篇实现修改密码模块。
同样地,我们首先得给修改密码创建表单(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篇 - 用户认证(修改密码)的更多相关文章
- [Django实战] 第3篇 - 用户认证(初始配置)
当大家打开一个网站时,第一步做什么?大部分一定是先登录吧,所以我们就从用户认证开始. 打开用户认证 Django本身已经提供了用户认证模块,使用它可以大大简化用户认证模块的开发,默认情况下,用户认证模 ...
- [Django实战] 第4篇 - 用户认证(用户登录)
今天来实现用户登录模块 首先,我们创建一个表单(forms.py): from django import forms from django.contrib.auth.models import U ...
- Django之auth模块(用户认证)
auth模块简介 auth模块是对登录认证方法的一种封装,之前我们获取用户输入的用户名及密码后需要自己从user表里查询有没有用户名和密码符合的对象, 而有了auth模块之后就可以很轻松的去验证用户的 ...
- Django之auth模块(用户认证)登陆组件
auth模块简介 auth模块是对登录认证方法的一种封装,之前我们获取用户输入的用户名及密码后需要自己从user表里查询有没有用户名和密码符合的对象, 而有了auth模块之后就可以很轻松的去验证用户的 ...
- 第十五篇 用户认证auth
用户认证auth 阅读目录(Content) 用户认证 auth模块 1 .authenticate() 2 .login(HttpRequest, user) 3 .logout(request) ...
- Ubuntu Server忘记密码后,单用户模式修改密码进去不了桌面的无奈
俗话说的好,好记性不如烂笔头.有时候脑子一热,就想不起来之前设置过的密码是什么了.我可怜地忘了我的Ubuntu Server的密码,回忆了n种组合都不行,于是只能进行单用户模式的修改密码了. 以下的操 ...
- linux 查看用户上次修改密码的日期【转】
1.找到以下文件: cat /etc/shadow 第三段字符就是最近一次密码修改的天数,此数字是距离1970年1月1日的天数. 2.用以下命令计算: date -u -d "1970- ...
- linux添加用户、修改密码
1.在root下添加用户用 adduser 命令 # 添加用户 admin [root@flm] #sudo adduser admin 2.添加用户登录密码 # 为用户 admin 修改密码 [ro ...
- 记一次CentOS7进单用户模式修改密码的失败经历(faild to load SELinux policy freezing)
背景:Cent SO7.4root用户密码忘记,根据https://www.linuxidc.com/Linux/2016-08/134034.htm提供的放法修改完密码之后系统启动后一直停留在转圈的 ...
随机推荐
- 在Delphi开发的服务中调用指定应用程序
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://fxh7622.blog.51cto.com/63841/529033 在很多时候 ...
- Starting the application on Mac does not work(拷贝platforms到不同的位置,才能解决问题),还可设置DYLD_PRINT_LIBRARIES=1 观察动态库
In some rare cases it can happen that the application does not launch and there is no reaction after ...
- (读书笔记).NET大局观-.NET框架类库概观
.NET框架类库概况 构建在.NET框架上所有的软件,都会用到通用语言进行时,即使基于最简单的CLR程序,也需要用到一部分.NET框架类库,更精致复杂的软件则使用这个类库提供的更多服务. .NET框架 ...
- mysql 监控 大批量的插入,删除,和修改
监控大批量的插入,修改和删除: mysql> insert into aaa select * from aaa; mysql> SELECT trx_id, trx_state, trx ...
- HDU-1664-Different Digits(BFS)
Problem Description Given a positive integer n, your task is to find a positive integer m, which is ...
- linux网络体系架构
原创kylin_zeng:http://blog.csdn.net/kylin_fire_zeng 本文参考国嵌视频教程,再此感谢国嵌教育. 一.协议栈层次对比: 1)网络接口层把数据链路层和物理层 ...
- 深入理解 Spring 事务原理【转】
本文转自码农网 – 吴极心原创 连接地址:http://www.codeceo.com/article/spring-transactions.html 一.事务的基本原理 Spring事务的本质其 ...
- Ultra Office Control 2.0
http://www.ultrashareware.com/Ultra-Office-Control.htm
- 基于visual Studio2013解决面试题之0901奇偶站队
题目
- 纯CSS设置Checkbox复选框控件的样式
Checkbox复选框是一个可能每一个网站都在使用的HTML元素,但大多数人并不给它们设置样式,所以在绝大多数网站它们看起来是一样的.为什么不把你的网站中的Checkbox设置一个与众不同的样式,甚至 ...