Window上python开发--4.Django的用户登录模块User
Android系统开发交流群:484966421 OSHome。
微信公众号:oshome2015
在搭建站点和web的应用程序时,用户的登录和管理是差点儿是每一个站点都必备的。
今天主要从一个实例了解下面django本身自带的user模块。
本文并不正确user进行扩展。
主要使用原生的模块。
1.User模块基础:
在使用user 之前先import到自己的iew中。相当与我们自己写好的models。仅仅只是这个是系统提供的models。
from django.contrib.auth.models import User # 导入user模块
1.1User对象属性
User 对象属性:username。 password(必填项)password用哈希算法保存到数据库
email,last_login,date_joined(字面意思就知道了)
is_staff ; 用户是否拥有站点的管理权限.
is_active : 是否同意用户登录, 设置为``False``,能够不用删除用户来禁止 用户登录
1.2User 对象方法
is_authenticated(): 假设是真正的 User 对象。返回值恒为 True 。 用于检查用户是否已经通过了认证。
通过认证并不意味着 用户拥有不论什么权限,甚至也不检查该用户是否处于激活状 态。这仅仅是表明用户成功的通过了认证。
这种方法非常重要, 在后台用request.user.is_authenticated()推断用户是否已经登录,假设true则能够向前台展示request.user.name
:set_password(passwd)
这种方法是用来更改password的,先用user=User.objects.get(username='')
user.set_password(passeord='')
user.save
check_password(passwd)
用户须要改动password的时候 首先要让他输入原来的password 。假设给定的字符串通过了password检查,返回 True
email_user(subj, msg)
给用户发送电子邮件,用 DEFAULT_FROM_EMAIL 的设 置作为发件人。也能够用第3个參数 from_email 来 覆盖设置。
1.3;创建User用户
使用 create_user 辅助函数创建用户:
from django.contrib.auth.models import User
user = User.objects.create_user(username='',password='',email='')
user.save 注意这里不是save()。1.4. 登录和认证
Django 在 django.contrib.auth 中提供了两个函数来处理这些事情—— authenticate() 和 login()
authenticate(): 认证给出的username和password。使用 authenticate() 函数。它接受两个參数,username username 和 password password ,并在password对用给出的username是合法的情况下返回一个 User 对象。当给出的password不合法的时候 authenticate() 函数返回 None
login() :该函数接受一个 HttpRequest 对象和一个 User 对象作为參数并使用Django的会话( session )框架把用户的ID保存在该会话中
from django.contrib import auth
user = auth.authenticate(username=username, password=password)
if user:
auth.login(request, user)
1.5.注销和重定向
注销 logout()该函数接受一个 HttpRequest 对象作为參数。没有返回值
auth.logout(request)重定向:HttpResponseRedirect()该函数主要实现,url的重定向。
在我们登录和注销后,重定向到指定url。该函数能够採用url的硬编码。
return HttpResponseRedirect('/sbook/sb_show')
2.实现用户注冊和登录
通过上面的基础知识,我们已经了解怎样创建和更新一个user啦。接下来用一个实例来做一下用户的注冊和登录。
案子mvc的模型。系统已经提供了model,所以我们要做的仅仅须要实现iew和template即可了。在view.py 中实现对注冊和登录的控制。
先看下面view中的代码
def alogin(request):
errors= []
account=None
password=None
if request.method == 'POST' :
if not request.POST.get('account'):
errors.append('Please Enter account')
else:
account = request.POST.get('account')
if not request.POST.get('password'):
errors.append('Please Enter password')
else:
password= request.POST.get('password')
if account is not None and password is not None :
user = authenticate(username=account,password=password)
if user is not None:
if user.is_active:
login(request,user)
return HttpResponseRedirect('/index')
else:
errors.append('disabled account')
else :
errors.append('invaild user')
return render_to_response('account/login.html', {'errors': errors})
def register(request):
errors= []
account=None
password=None
password2=None
email=None
CompareFlag=False if request.method == 'POST':
if not request.POST.get('account'):
errors.append('Please Enter account')
else:
account = request.POST.get('account')
if not request.POST.get('password'):
errors.append('Please Enter password')
else:
password= request.POST.get('password')
if not request.POST.get('password2'):
errors.append('Please Enter password2')
else:
password2= request.POST.get('password2')
if not request.POST.get('email'):
errors.append('Please Enter email')
else:
email= request.POST.get('email') if password is not None and password2 is not None:
if password == password2:
CompareFlag = True
else :
errors.append('password2 is diff password ') if account is not None and password is not None and password2 is not None and email is not None and CompareFlag :
user=User.objects.create_user(account,email,password)
user.is_active=True
user.save
return HttpResponseRedirect('/account/login') return render_to_response('account/register.html', {'errors': errors}) def alogout(request):
logout(request)
return HttpResponseRedirect('/index')
从以上的代码中。我们是在template里创建的form。
在templates下创建account文件夹。在以下创建login.html
<!DOCTYPE html>
<html>
<head>
<title>Welcome login </title>
</head>
<body>
<p>Account Login </p> {% if errors %}
<li>
{% for error in errors %}
<p style="color: red;">
Please correct the error: {{error}} below.
</p>
{% endfor %}
</li>
{% endif %} <form action="" method="post">
<input type = 'text' placeholder="Please input account" name="account">
<br> <input type = 'password' placeholder="Please input password" name="password">
<br>
<input type = 'submit' placeholder="Login" value="Login">
<br>
<a href="/account/register">register new accout</a>
</form>
</body>
</html>
相同的方式创建register.html
<html>
<head>
<title>Welcome Register New Account</title>
</head>
<body> {% if errors %}
<li> {% for error in errors %}
<p style="color: red;">
Please correct the error: {{error}} below.
</p>
{% endfor %}
</li>
{% endif %}
<table>
<form action="" method="post">
<tr>
<td>
<label >Account:</label>
</td>
<td>
<input type = 'text' placeholder="Please input account" name = 'account'>
</td>
</tr>
<tr>
<td>
<label >Password:</label>
</td>
<td>
<input type = 'password' placeholder="Please input password" name = 'password'>
</td>
</tr>
<tr>
<td>
<label >Password:</label>
</td>
<td>
<input type = 'password' placeholder="Please input password" name ='password2'>
</td> </tr>
<tr>
<td>
<label>email:</label>
</td>
<td>
<input type="email" placeholder="Please input email" name = 'email'>
</td>
</tr>
<tr> <td>
<input type = 'submit' placeholder="Login" value="Login">
</td>
</tr>
</form>
</table>
</body>
</html>
接下来view和template创建好了。仅仅有床urls的映射关系啦。
url(r'^account/login/$', alogin),
url(r'^account/register/$', register),
url(r'^account/logout/$', alogout),
ok到此为止,用户的注冊和登录就能够在在浏览器上看到效果啦。
Window上python开发--4.Django的用户登录模块User的更多相关文章
- 项目开发-->身份认证及用户登录模块
1.首先明确的两个问题 如何判断当前申请是由一个已登录用户发起的?如果Request.IsAuthenticated为true,则表示是一个已登录用户. 如何获取当前登录用户的登录名?如果是一个已登录 ...
- Window上python 开发--1.搭建开发环境
事实上在开发python最好在ubuntu环境下,简单也便于扩展各个package.可是我的linux的电脑临时不在身边.还的我老婆的电脑win7没办法啊. 因为python的跨平台性.在window ...
- Windows上python开发--2安装django框架
Windows上python开发--2安装django框架 分类: 服务器后台开发2014-05-17 21:22 2310人阅读 评论(2) 收藏 举报 python django 上一篇文章中讲了 ...
- unbuntu16.04上python开发环境搭建建议
unbuntu16.04上python开发环境搭建建议 2017-12-20 10:39:27 推荐列表: pycharm: 可以自行破解,但是不推荐,另外也不稳定 pydev+eclipse: ...
- Django中用户权限模块
Django中用户权限模块 1 auth模块 auth模块是Django提供的标准权限管理系统,可以提供用户身份认证, 用户组和权限管理. auth可以和admin模块配合使用, 快速建立网站的管理系 ...
- Django:用户登录实例
Django:用户登录实例 一.源代码 1,login.html代码(登录界面): <!DOCTYPE html> <html lang="zh-CN"> ...
- python入门:模拟简单用户登录(自写)
#!/usr/bin/env python # -*- coding: utf-8 -*- #模拟简单用户登录(自写) import getpass a = raw_input("Pleas ...
- Java SSH框架系列:用户登录模块的设计与实现思路
1.简介用户登录模块,指的是根据用户输入的用户名和密码,对用户的身份进行验证等.如果用户没有登录,用户就无法访问其他的一些jsp页面,甚至是action都不能访问.二.简单设计及实现本程序是基于Jav ...
- Python开发【Django】:Model操作(二)
Model操作 1.操作汇总: # 增 # # models.Tb1.objects.create(c1='xx', c2='oo') 增加一条数据,可以接受字典类型数据 **kwargs # obj ...
随机推荐
- HOJ 2645 WNim sg函数 博弈论
http://blog.csdn.net/y1196645376/article/details/52165245 这道题我没有写,因为我就算翻译了我也找不到数据范围,也分不清数据变量的命名,而且ho ...
- ( VIJOS )VOJ 1049 送给圣诞夜的礼品 矩阵快速幂
https://vijos.org/p/1049 非常普通的矩阵快速幂... 但是我 第一次写忘了矩阵不能交换律... 第一二次提交RE直到看到题解才发现这道题不能用递归快速幂... 第三次提交成 ...
- 扩展gcd codevs 1200 同余方程
codevs 1200 同余方程 2012年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description 求关 ...
- 扩展gcd codevs 1213 解的个数
codevs 1213 解的个数 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 已知整数x,y满足如下面的条件: ax+by ...
- ACM -- 算法小结(四)KMP(POJ3461)
KMP -- POJ3461解题报告 问题描述:给出字符串P和字符串T,问字符串P在字符串T中出现的次数 Sample Input 3 BAPC BAPC AZA AZAZAZA VERDI ...
- [转]Jquery实现页面定时跳转
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 集成学习中的 stacking 以及python实现
集成学习 Ensemble learning 中文名叫做集成学习,它并不是一个单独的机器学习算法,而是将很多的机器学习算法结合在一起,我们把组成集成学习的算法叫做“个体学习器”.在集成学习器当中,个体 ...
- 基于CDH,部署Apache Kylin读写分离
一. 部署读写分离的契机 目前公司整体项目稳定运行在CDH5.6版本上,与其搭配的Hbase1.0.0无法正确运行Kylin,原因是Kylin只满足Hbase1.1.x+版本.解决方案如下 1. 升级 ...
- UML建模之时序图(Sequence Diagram)教程
一.时序图 时序图是一种强调时间顺序的交互图,在时序图中,首先把参与交互的对象放在图的上方,沿X轴方向排列.通常把发起交互的对象放在左边,较下级对象依次放在 右边,然后把这些对象发送和接受的消息沿Y轴 ...
- Dual transistor improves current-sense circuit
In multiple-output power supplies in which a single supply powers circuitry of vastly different curr ...