Auth模块、Forms组件
Auth模块
auth模块是Django自带的用户认证模块:
我们在开发一个网站的时候,无可避免的需要设计实现网站的用户系统。此时我们需要实现包括用户注册、用户登录、用户认证、注销、修改密码等功能,这还真是个麻烦的事情呢。
Django作为一个完美主义者的终极框架,当然也会想到用户的这些痛点。它内置了强大的用户认证系统--auth,它默认使用 auth_user 表来存储用户数据。
为了方便快捷的帮开发者完成登录相关的认证交互功能。
auth_user表常用操作:
from django.contrib.auth.models import User
#创建普通用户
User.objects.create_user(username='Owen', password='') #创建超级用户
User.objects.create_superuser(username='root',password='root',email='root@root.com') #获取第一个用户
user = User.objects.first() #修改密码
user.set_password('')
user.save() #注:修改完成后必须要进行保存,即执行save()方法 #校验密码
res = user.check_password('')
auth组件常用功能:
# 校验用户账号及密码,校验成功返回user对象
from django.contrib.auth import authenticate
#该模块需要两个参数,用户名和密码
user=authenticate(username=usr, password=pwd) # 注册用户到request对象中,注册成功可以用request.user访问当前登录用户(会形成session记录)
from django.contrib.auth import login
login(request, user) # 注册authenticate成功(当前登录)的用户 # 注销当前注册的user(用户注销)
from django.contrib.auth import logout
logout(request) # 校验用户登录状态
# 视图函数中使用
if request.user.is_authenticated(): pass
# 模板语言中使用
{% if request.user.is_authenticated %}
{% else %}
{% endif %} # 校验登录状态的装饰器
from django.contrib.auth.decorators import login_required
@login_required(login_url='/user_login/')
def user_home(request):
return render(request, 'user.html', locals())
若用户没有登录,则会跳转到django默认的 登录URL '/accounts/login/ ' 并传递当前访问url的绝对路径 (登陆成功后,会重定向到该路径)。
如果需要自定义登录的URL,则需要在settings.py文件中通过LOGIN_URL进行修改。
# 在settings.py中设置:
LOGIN_URL = '/login/' # 这里配置成你项目登录页面的路由
扩展User表:
# app/models.py
#第一种方式:(建议使用)
from django.contrib.auth.models import AbstractUser
class User(AbstractUser):
# 增加自定义字段
info = models.TextField(null=True) # settings.py配置
AUTH_USER_MODEL = 'app.User' # 在视图函数中导入的模块就是:
from app.models import User #第二种方式:(不建议使用)
from django.contrib.auth.models import User
Create your models here.
class UserInfo(models.Model):
id = models.AutoField(primary_key=True)
info = models.TextField(null=True)
user = models.OneToOneField(to=User, to_field='username', db_constraint=False, null=True, on_delete=models.SET_NULL)
Forms组件
表单字段的校验:
<!-- register.html核心代码 -->
<form action="" method="post" novalidate>
<input type="text" name="usr">
<input type="password" name="pwd">
<input type="email" name="email">
<input type="submit" value="注册">
</form>
# views.py核心代码
from django.shortcuts import render, HttpResponse
from django import forms
# 自定义校验表单字段的类,继承forms.Form,并用forms下具体字段完成校验
class CheckForm(forms.Form):
# 通过error_messages自定义错误信息
usr = forms.CharField(min_length=3, max_length=10, error_messages={'min_length':'长度至少为3'})
pwd = forms.CharField(min_length=3, max_length=10)
email = forms.EmailField(error_messages={'invalid':'邮箱不合法', 'required': '必填项'}) def register(request):
if request.method == "GET":
return render(request, 'register.html')
if request.method == "POST":
# 校验请求的所有数据
check_form = CheckForm(request.POST)
if check_form.is_valid():
# 查看校验成功的数据,为字典类型
print(check_form.cleaned_data)
return HttpResponse('注册成功')
else:
# 查看校验失败的数据,为封装的字典类型
print(check_form.errors)
return HttpResponse('注册失败')
表单元素的渲染:
# view.py改动代码
class CheckForm(forms.Form):
usr = forms.CharField(min_length=3, max_length=10, label="用户名")
pwd = forms.CharField(min_length=3, max_length=10, label="密码")
email = forms.EmailField(, label="邮箱") def register(request):
if request.method == "GET":
check_form = CheckForm()
return render(request, 'register.html', {'check_form': check_form})
<!-- register.html核心代码 -->
<!-- 方式一 -->
<form action="" method="post">
{{ check_form.usr }}
{{ check_form.pwd }}
{{ check_form.email }}
<input type="submit" value="注册">
</form>
<!-- 方式二 -->
<form action="" method="post">
{% for foo in check_form %}
<label for="{{ foo.id_for_label }}" class="col-sm-2 control-label">{{ foo.label }}</label>
{{ foo }}
{% endfor %}
<input type="submit" value="注册">
</form>
<!-- 方式三 -->
<form action="" method="post">
<table>{{ check_form.as_table}}</table>
<input type="submit" value="注册">
</form>
<!-- 方式四 -->
<form action="" method="post">
<ul>{{ check_form.as_ul}}</ul>
<input type="submit" value="注册">
</form>
<!-- 方式五 -->
<form action="" method="post">
{{ check_form.as_p}}
<input type="submit" value="注册">
</form>
错误信息的渲染:
# views.py
class CheckForm(forms.Form):
usr = forms.CharField(
min_length=3,
max_length=10,
error_messages={
'min_length': '长度至少为3',
'max_length': '长度最多为10',
'required': '必填项'
},
label="用户名"
)
pwd = forms.CharField(
min_length=3,
max_length=10,
error_messages={
'min_length': '长度至少为3',
'max_length': '长度最多为10',
'required': '必填项'
},
label="密码"
)
email = forms.EmailField(
error_messages={
'invalid': '邮箱不合法',
'required': '必填项'}
) def register(request):
if request.method == "GET":
check_form = CheckForm()
if request.method == "POST":
check_form = CheckForm(request.POST)
if check_form.is_valid():
return HttpResponse('注册成功')
return render(request, 'register.html', locals())
<form action="" method="post" novalidate>
{% for ele in check_form %}
<p>
{{ ele.label }}:{{ ele }}
<span style="color: red">{{ ele.errors.0 }}</span>
</p>
{% endfor %} <input type="submit" value="注册">
</form>
组件的参数设置:
class Ret(Form):
name = forms.CharField(max_length=10, min_length=2, label='用户名',
error_messages={'required': '该字段不能为空', 'invalid': '格式错误', 'max_length': '太长',
'min_length': '太短'},
widget=widgets.TextInput(attrs={'class':'form-control'}))
pwd = forms.CharField(max_length=10, min_length=2, widget=widgets.PasswordInput(attrs={'class':'form-control'}))
email = forms.EmailField(label='邮箱', error_messages={'required': '该字段不能为空', 'invalid': '格式错误'})
局部钩子:
# 在自定义验证类CheckForm中添加局部验证钩子
class CheckForm(forms.Form):
...
def clean_usr(self):
name = self.cleaned_data.get('usr') # type: str
import re
if re.match('^[0-9]', name):
from django.core.exceptions import ValidationError
raise ValidationError('不能以数字开头')
return name
全局钩子:
# views.py
class CheckForm(forms.Form):
usr = forms.CharField(
min_length=3,
max_length=10,
error_messages={
'min_length': '长度至少为3',
'max_length': '长度最多为10',
'required': '必填项'
},
label="用户名",
widget=forms.TextInput(attrs={'placeholder': '请输入用户名'})
)
pwd = forms.CharField(
min_length=3,
max_length=10,
error_messages={
'min_length': '长度至少为3',
'max_length': '长度最多为10',
'required': '必填项'
},
label="密码",
widget=forms.PasswordInput(attrs={'placeholder': '请输入密码'})
)
re_pwd = forms.CharField(
min_length=3,
max_length=10,
error_messages={
'min_length': '长度至少为3',
'max_length': '长度最多为10',
'required': '必填项'
},
label="确认密码",
widget=forms.PasswordInput(attrs={'placeholder': '请确认密码'})
)
def clean(self):
pwd = self.cleaned_data.get('pwd')
re_pwd = self.cleaned_data.get('re_pwd')
if pwd == re_pwd:
return self.cleaned_data
from django.core.exceptions import ValidationError
raise ValidationError('两次密码不一致') def register(request):
if request.method == "GET":
check_form = CheckForm()
if request.method == "POST":
check_form = CheckForm(request.POST)
if check_form.is_valid():
return HttpResponse('注册成功')
else:
# 拿到全局钩子抛出的错误信息
all_error = check_form.errors.get('__all__', None)
return render(request, 'register.html', locals())
<form action="" method="post" novalidate>
{% for ele in check_form %}
<p>
{{ ele.label }}:{{ ele }}
<span style="color: red">{{ ele.errors.0 }}</span>
{% if ele.label == '确认密码' %}
<span style="color: red">{{ all_error.0 }}</span>
{% endif %}
</p>
{% endfor %}
<input type="submit" value="注册">
</form>
Auth模块、Forms组件的更多相关文章
- Auth组件,Forms组件
一.Auth组件默认auth_user表常用操作 #1.配置settings,使django与数据库连接 DATABASES = { 'default': { 'ENGINE': 'django.db ...
- Django:forms局部函数、cookie、sesion、auth模块
一.forms组件 二.cookie和session组件 三.auth组件 一.forms组件 1.校验字段功能 针对一个实例:注册用户讲解 模型:models class UserInfo(mode ...
- django第13天(auth组件,forms组件,中间件,csrf)
django第13天(auth组件,forms组件) auth组件 -auth组件 -auth是什么? -django内置的用户认证系统,可以快速的实现,登录,注销,修改密码.... -怎么用? -( ...
- Django组件(四) Django之Auth模块
Auth模块概述 Auth模块是Django自带的用户认证模块: 我们在开发一个网站的时候,无可避免的需要设计实现网站的用户系统.此时我们需要实现包括用户注册.用户登录.用户认证.注销.修改密码等功能 ...
- Django之auth模块(用户认证)登陆组件
auth模块简介 auth模块是对登录认证方法的一种封装,之前我们获取用户输入的用户名及密码后需要自己从user表里查询有没有用户名和密码符合的对象, 而有了auth模块之后就可以很轻松的去验证用户的 ...
- Web框架之Django_10 重要组件(Auth模块)
一.auth模块介绍 Auth模块是Django自带的用户认证模块: 我们在开发一个网站的时候,无可避免的需要设计实现网站的用户系统.此时我们需要实现包括用户注册.用户登录.用户认证.注销.修改密码等 ...
- python 全栈开发,Day78(Django组件-forms组件)
一.Django组件-forms组件 forms组件 django中的Form组件有以下几个功能: 生成HTML标签 验证用户数据(显示错误信息) HTML Form提交保留上次提交数据 初始化页面显 ...
- BBS-基于forms组件和ajax实现注册功能
http://www.cnblogs.com/yuanchenqi/articles/7638956.html 1.设计注册页面 views.py from django import forms c ...
- Django-多对多关系的三种创建方式-forms组件使用-cookie与session-08
目录 表模型类多对多关系的三种创建方式 django forms 组件 登录功能手写推理过程 整段代码可以放过来 forms 组件使用 forms 后端定义规则并校验结果 forms 前端渲染标签组件 ...
随机推荐
- 精通Dubbo——Dubbo支持的协议的详解
转: 精通Dubbo——Dubbo支持的协议的详解 2017年06月02日 22:26:57 孙_悟_空 阅读数:44500 Dubbo支持dubbo.rmi.hessian.http.webse ...
- openssl命令行将pfx格式转.key和.crt文件,Apache适用
以前使用的windows平台作为php运行的平台,感觉整体速度太慢,于是现在改为centos系统的linux平台.需要把windows中用pfx证书转为appache用到key和crt组成的证书 将* ...
- Linux设备树(四 中断)
四 中断 中断一般包括中断产生设备和中断处理设备.中断控制器负责处理中断,每一个中断都有对应的中断号及触发条件.中断产生设备可能有多个中断源,有时多个中断源对应中断控制器中的一个中断,这种情况中断产生 ...
- postgres 基本操作
登陆: $ psql -U <user> -d <dbname> 数据库操作: $ \l //查看库 $ \c <dbname> //切换库 // ...
- luogu 4042 有后效性的dp
存在有后效性的dp,但转移方程 f[i] = min( f[i], s[i] + sigma f[j] ( j 是后效点) ) 每次建当前点和 转移点的边 e1, 某点和其会影响的点 e2 spfa ...
- python从爬虫基础到爬取网络小说实例
一.爬虫基础 1.1 requests类 1.1.1 request的7个方法 requests.request() 实例化一个对象,拥有以下方法 requests.get(url, *args) r ...
- laravel 跨库执行原生 sql 语句
执行原生 sql 返回结果集
- Django之文件上传
一.form表单上传文件 注意: 1.form上需要加enctype="multipart/form-data" 2.form提交的地址需要以/结尾 def form_file(r ...
- 【算法】【python实现】二叉搜索树插入、删除、查找
二叉搜索树 定义:如果一颗二叉树的每个节点对应一个关键码值,且关键码值的组织是有顺序的,例如左子节点值小于父节点值,父节点值小于右子节点值,则这棵二叉树是一棵二叉搜索树. 类(TreeNode):定义 ...
- Okhttp同步请求源码分析
进阶android,OKhttp源码分析——同步请求的源码分析 OKhttp是我们经常用到的框架,作为开发者们,我们不单单要学会灵活使用,还要知道他的源码是如何设计的. 今天我们来分析一下OKhttp ...