[py][mx]django form验证-给db减压
django form认证-解压db压力
- 一般系统都需要前后端都验证
- 前端验证容器逃逸破解,如通过js console口去发
试想如果后端只有db验证,那么前端无论发什么后端都查询一次db,对db压力太大, 所以后端 先通过form验证,对其长度等验证通过后才db验证.
新建forms.py
forms.py里的字段要和前端的login表单字段name对应上
users/forms.py
from django import forms
class LoginForm(forms.Form):
username = forms.CharField(required=True)
password = forms.CharField(required=True)
users/viewspy
from django.contrib.auth import authenticate, login
from django.contrib.auth.backends import ModelBackend
from django.db.models import Q
from django.shortcuts import render
from django.views.generic import View
# Create your views here.
from users.forms import LoginForm
from users.models import UserProfile
class UserView(View): # 新的login view. 继承了View类,它里面实现get post等方法, 使用类模式写免去了函数模式的判断
def get(self, request):
return render(request, "login.html", {})
def post(self, request):
login_form = LoginForm(request.POST) # 传递进来的字段先进行表单验证,如果规则通过在进入查库逻辑
if login_form.is_valid():
user_name = request.POST.get("username", "") # 字典取值,如果无,赋值为空
pass_word = request.POST.get("password", "")
user = authenticate(username=user_name, password=pass_word)
if user is not None: # 用户名密码验证成功
login(request, user) # django执行用户登录
return render(request, "index.html")
else:
return render(request, "login.html", {'msg': "用户名或密码错误"})
else:
return render(request, "login.html", {'msg': "用户名或密码不符合规则"})
此时如果前端什么都不输入提交
debug模式看到
返回form报错到前端
users/views.py
class UserView(View): # 新的login view. 继承了View类,它里面实现get post等方法, 使用类模式写免去了函数模式的判断
def get(self, request):
return render(request, "login.html", {})
def post(self, request):
login_form = LoginForm(request.POST) # 传递进来的字段先进行表单验证,如果规则通过在进入查库逻辑
if login_form.is_valid():
user_name = request.POST.get("username", "") # 字典取值,如果无,赋值为空
pass_word = request.POST.get("password", "")
user = authenticate(username=user_name, password=pass_word)
if user is not None: # 用户名密码验证成功
login(request, user) # django执行用户登录
return render(request, "index.html")
else:
return render(request, "login.html", {'msg': "用户名或密码错误"})
else:
return render(request, "login.html", {'msg': "用户名或密码不符合规则", "login_form": login_form}) # 将django的form验证失败内置信息发给前端展示用
templates/login.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>login</title>
</head>
<body>
<div>
<form action="/login/" method="post">
<p><input type="text" name="username" placeholder="username"></p>
<p><input type="text" name="password" placeholder="password"></p>
<p><input type="submit"></p>
{% csrf_token %}
</form>
{% if login_form.errors.username %}
{% for key,value in login_form.errors.items %}
{{ key }}: {{ value }}
{% endfor %}
{% endif %}
{{ msg }}
</div>
</body>
</html>
也可以单独把erros提取出来返回给前端, 如error_msg = user_input_obj.errors
form有2个作用: 1, 验证 2,生成html(另一种写法了)
if user_input_obj.is_valid():#form验证通过
...
else:
error_msg = user_input_obj.errors
return render(request, "user_list.html", {'obj': user_input_obj, 'errors': error_msg})#错误信息返回
前端页面:
<form action="/user_list/" method="post">
<p>用户类型: {{ obj.user_type }} <span>{{ errors.user_type }}</span></p>
....
{% csrf_token %}
</form>
[py][mx]django form验证-给db减压的更多相关文章
- [py][mx]django自定义认证类-实现邮箱作为用户名登录
创建自定义验证用户名密码类CustomBackend users/views.py from django.contrib.auth import authenticate, login from d ...
- [py][mx]django处理登录逻辑
浏览器同源策略(same-origin policy) csrf攻击防御核心点总结 django的cookie和session操作-7天免登录 flask操作cookie&django的see ...
- Django form验证
# 模版 class LoginForm(forms.Form): # 模版中的元素 user = forms.CharField(min_length=6,error_messages={" ...
- [py][mx]django使用class写views-免去判断方法的烦恼
修改views使用class模式 类模式写views - 免去了函数模式的判断的烦恼 users/views.py from django.views.generic import View clas ...
- [py][mx]django项目-让系统用自定义的users表认证
项目开端 参考的是mxonline项目 先把这两项完成 1.app设计 2.app的models的设计 经过分析系统有四个模块 users - 用户管理 course - 课程管理 oranizati ...
- [py][mx]django课程页显示city和机构封面图
city和课程机构信息展示到前台去 organization/views.py from django.views.generic.base import View from organization ...
- [py][mx]django模板继承-课程列表页
课程列表页分析 1,机构类型 2,所在地区 3.排序 学习人数 先分析下 纵观页面,页头页脚都一样. django提供了模板继承. 至少 不同页面的title 面包屑路径 content内容不一致,以 ...
- [py][mx]django xadmin后台配置
xadmin配置 - 安装 pip install -r https://github.com/sshwsfc/xadmin/blob/django2/requirements.txt 以下被我测试通 ...
- [py][mx]django邮箱注册的验证码部分-django-simple-captcha库使用
邮箱注册-验证码 验证码使用第三方库django-simple-captcha 这个安装图形插件步骤官网有哦 - 1.Install django-simple-captcha via pip: pi ...
随机推荐
- 【laravel5.6】laravel 自定义公共函数
1.在 app/Helpers/ 新建一个文件 functions.php,当然这个文件位置和名称你可以自己定义,创建一些函数用于全局调用: 2.在composer.json中的autoload下增加 ...
- 浅谈CSS盒子模型
[摘要]盒子模型是CSS中的一个重要概念,虽然CSS中没有盒子这个单独的属性对象,但它却是CSS中无处不在的一个重要组成部分.掌握盒子模型的原理和使用方法可以极大地丰富HTML元素的表现效果,同时对于 ...
- 深入理解 Neutron -- OpenStack 网络实现(1):GRE 模式
问题导读1.什么是VETH.qvb.qvo?2.qbr的存在的作用是什么?3.router服务的作用是什么? 如果不具有Linux网络基础,比如防火墙如何过滤ip.端口或则对openstack ovs ...
- 关于 CommonJS AMD CMD UMD 规范的差异总结(转)
根据CommonJS规范,一个单独的文件就是一个模块.每一个模块都是一个单独的作用域,也就是说,在一个文件定义的变量(还包括函数和类),都是私有的,对其他文件是不可见的. // foo.js var ...
- virgo-tomcat-server的生产环境线上配置与管理
Virgo Tomcat Server简称VTS,VTS是一个应用服务器,它是轻量级, 模块化, 基于OSGi系统.与OSGi紧密结合并且可以开发bundles形式的Spring web apps应用 ...
- RSA加密工具类(非对称加密算法)
import com.jfinal.log.Log;import org.apache.commons.codec.binary.Base64; import javax.crypto.Cipher; ...
- hiredis安装及测试
(1) redis环境搭建 (2) hiredis下载地址及C API github (3) hiredis安装 我是把libhiredis.so放到/usr/local/lib/中, ...
- jquery 1.9 1.8 判断 浏览器(IE11,IE8,IE7,IE6)版本
1.9以后很我方法删除了,所有和之前版本判断浏览器版本有所差记录一下 1.9 ------------------------------------------------------------- ...
- Memcached 简单利用和简单了解(Mac的安装和使用)
Memcached 是一种用于分布式应用的一种缓存机制.应用也比较广泛.这里来学习一下. 首先Memcached 是分布式网站架构都需要用到的缓存机制.缓存就是服务器利用多余的空间上开辟了一个储存空间 ...
- HUST 1599 - Multiple(动态规划)
1599 - Multiple 时间限制:2秒 内存限制:64兆 461 次提交 111 次通过 题目描述 Rocket323 loves math very much. One day, Rocke ...