django 之(五) --- 验证码|富文本|邮箱短信
验证码
在用户登录,注册以及一些敏感操作的时候,我们为了防止服务器被暴力请求,或爬虫爬取,我们可以使用验证码进行过滤,减轻服务器的压力。
原生实现:
- 库名:pip install Pillow 验证码需要使用绘图 Pillow
- 核心:Image(画布)、ImageDraw(画笔)、ImageFont(画笔修饰)
- urls.py 和 settings.py
# --------------- urls.py------------------
from django.conf.urls import url
from App import views urlpatterns = [
url(r'^getcode/',views.get_code,name='get_code'),
] # --------------settings.py-----------------
# 注册字体路径
FONT_PATH = os.path.join(BASE_DIR, 'static/fonts/ADOBEARABIC-BOLD.OTF')
- views.py
import random
from io import BytesIO
from PIL import Image, ImageFont
from PIL.ImageDraw import ImageDraw
from django.http import HttpResponse
from App.utils import get_color, generate_code
from DjangoCache import settings # &&&&&&&&&&& 验证码绘制 &&&&&&&&&
def get_code(request):
mode = 'RGB'
size = [200, 100]
red = get_color()
green = get_color()
blue = get_color()
color_bg = (red, green, blue) # 初始化 画布和画笔
# 画布(颜色模式rgb,画布尺寸,颜色)
image = Image.new(mode, size=size, color=color_bg)
# 画笔(画布,模式rgb)
imagedraw = ImageDraw(image, mode=mode)
# 构造字体样式(路径,默认值)
imagefont = ImageFont.truetype(settings.FONT_PATH, 100)
verify_code = generate_code()
request.session['verify_code'] = verify_code # 将验证码存入存sesssion for i in range(4):
fill = (get_color(), get_color(), get_color())
# 画图(坐标,画什么,字体,颜色)
imagedraw.text(xy=(50 * i, 0), text=verify_code[i], font=imagefont, fill=fill) for i in range(10000):
fill = (get_color(), get_color(), get_color())
xy = (random.randrange(201), random.randrange(100))
# 画干扰点(位置随机,颜色随机)
imagedraw.point(xy=xy, fill=fill)
# 设置字节内存流IO流
fp = BytesIO()
# 存入(在内存流,存入格式)
image.save(fp, 'png')
# fp.getvalue从内存中获取值 传递给前端
return HttpResponse(fp.getvalue(), content_type='image/png') # 将以下两个函数 放入工具类中。使用时导入
# 设置颜色。每次不同
def get_color():
return random.randrange(256) # 设置文字,每次不同
def generate_code():
source = 'qwertyuiopasdfghjklzxcvbnm1234567890'
code = ''
for i in range(4):
code += random.choice(source)
return code
@csrf_exempt
def login(request):
if request.method == 'GET':
return render(request, 'login.html') elif request.method == 'POST':
receive_code = request.POST.get('verify_code')
store_code = request.session.get('verify_code') if receive_code != store_code: # 判断验证码是否正确
return HttpResponseRedirect(reverse('app:login'))
return HttpResponse('登陆成功')
- login.html
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>用户登陆</title>
<script src="https://cdn.bootcss.com/jquery/1.11.1/jquery.js"></script>
<script type="text/javascript" src="{% static 'js/login.js' %}"></script>
</head>
<body>
<form action="{% url 'app:login' %}" method="post">
<span>用户名:</span><input type="text" name="username" placeholder="精神精神">
<br>
<spam>验证码:</spam><input type="text" name="verify_code" placeholder="请输入下图验证码">
<br>
<img src="{% url 'app:get_code' %}" >
<button>登陆</button>
</form>
</body>
</html>
富文本
介绍:
富文本:Rich Text Format(RTF),是有微软开发的跨平台文档格式,大多数的文字处理软件
都能读取和保存RTF文档,其实就是可以添加样式的文档,和HTML有很多相似的地方
插件:
- 名称:tinymce 插件
- 安装:pip install django-tinymce
场景:
- 在后台管理中使用
- 在页面中使用,通常用来作博客
使用:
- 配置,settings.py文件中
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'App',
'tinymce', # 注册
] # 配置
TINYMCE_DEFAULT_CONFIG = {
'theme': 'advanced', # 主题样式
'width': 800, # 宽度
'height': 600, # 高度
}
- 应用
- models.py 配置模型并迁移同步。HTMLfield()继承自models.TextField
from django.db import models
from tinymce.models import HTMLField class Blog(models.Model):
b_content = HTMLField()
- urls.py 和 views.py 路由和试图
from django.conf.urls import url
from App import views urlpatterns = [
url(r'^editblog/',views.edit_blog,name='edit_blog'),
] # ================================= from django.shortcuts import render def edit_blog(request):
return render(request,'edit_blog.html')
- edit_blog.html
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>EditBlog</title>
<script type="text/javascript" src="/static/tiny_mce/tiny_mce.js"></script>
<script type="text/javascript">
tinyMCE.init({
"mode": "textareas",
"theme": "advanced",
"width": 800,
"height": 600
})
</script>
</head>
<body>
<form action="{% url 'app:edit_blog' %}" method="post">
{% csrf_token %}
<textarea name="content"></textarea>
<button>保存</button>
</form>
</body>
</html>
邮箱认证
短信认证
django 之(五) --- 验证码|富文本|邮箱短信的更多相关文章
- Django (八) 中间件&验证码&富文本&缓存
中间件&验证码&富文本&缓存 1. 中间件&AOP 中间件:是一个轻量级的,底层的插件,可以介入Django的请求和响应过程(面向切面编程) 中间件的本质就是一 ...
- CactiEZ 中文版V10.1安装使用以及139邮箱短信报警设置
说明:CactiEZ中文版V10.1是基于CentOS 6.0系统,整合Cacti等相关软件,重新编译而成的一个操作系统! 说明:CactiEZ中文版V10.1是基于CentOS 6.0系统,整合 ...
- 在django中集成ckeditor富文本
目前用的比较多的富文本插件有百度的ueditor.ckeditor.kindeditor等,其中ueditor和kindeditor比较美观,ckeditor的皮肤较少.但是ueditor加载较慢,k ...
- django—xadmin中集成富文本编辑器ueditor
一.安装 pip命令安装,由于ueditor为百度开发的一款富文本编辑框,现已停止维护,如果解释器为python2,则直接pip install djangoueditor 解压包安装,python3 ...
- django xadmin 集成DjangoUeditor富文本编辑器
本文档记录自己的学习历程! 介绍 Ueditor HTML编辑器是百度开源的在线HTML编辑器,功能非常强大 额外功能 解决图片视频等无法上传显示问题 Ueditor下载地址 https://gith ...
- Django xadmin后台添加富文本编辑器UEditor的用法
效果图: 步骤: 1.利用命令:pip install DjangoUeditor,安装DjangoUeditor,但由于DjangoUeditor没有python3版本的,从的Github上把修改好 ...
- django中使用tinymce 富文本
django后台集成富文本编辑器Tinymce 安装方式一: 1.首先去python的模块包的网站下载一个django-tinymce的包 https://pypi.python.org/pypi/ ...
- Django使用xadmin集成富文本编辑器Ueditor(方法二)
一.xadmin的安装与配置1.安装xadmin,其中第一种在python3中安装不成功,推荐第二种或者第三种 方式一:pip install xadmin 方式二:pip install git+g ...
- Django项目中添加富文本编辑器django-ckeditor
django-ckeditor库的使用步骤: 1.在命令行下安装django-ckeditor这个库: 命令:pip install django-ckeditor 2.安装成功后,配置Django项 ...
随机推荐
- 转 java 中int String类型转换
转自licoolxue https://blog.csdn.net/licoolxue/article/details/1533364 int -> String int i=12345;Str ...
- 特殊方法 之 len __repr__ __str__
关于len, 如果x是一个内置类型的实例,那么len(x)的速度回非常快,背后的原因是CPython会直接从一个C结构体里读取对象的长度,完全不用调用任何方法,获取一个集合中的元素的数量是一个很常见的 ...
- 2018 南京网络预赛Sum - 线性筛
题意 链接 定义 $f(x)$ 为满足以下条件的有序二元组 $(a, b)$ 的方案数(即 $(a, b)$ 与 $(b, a)$ 被认为是不同的方案): $x= ab$ $a$ 和 $b$ 均无平方 ...
- C# 任务、线程、同步(四)
Timer 类使用 static void Main(string[] args) { ThreadingTimer(); TimersTimer(); Console.Read(); } stati ...
- deferred.pipe([doneFilter],[failFilter],[progressFilter])
deferred.pipe([doneFilter],[failFilter],[progressFilter]) 概述 筛选器和/或链Deferreds的实用程序方法. deferred.pipe( ...
- click([[data],fn]) 触发每一个匹配元素的click事件。
click([[data],fn]) 概述 触发每一个匹配元素的click事件. 这个函数会调用执行绑定到click事件的所有函数.大理石平台精度等级 参数 fnFunctionV1.0 在每一个匹配 ...
- [Luogu] 让我们异或吧
https://www.luogu.org/problemnew/show/P2420 异或满足 A ^ B = B ^ A A ^ A = 0 0 ^ A = A #include <cstd ...
- 2018 Nowcoder Multi-University Training Contest 1
Practice Link J. Different Integers 题意: 给出\(n\)个数,每次询问\((l_i, r_i)\),表示\(a_1, \cdots, a_i, a_j, \cdo ...
- 走进JavaWeb技术世界2:JSP与Servlet的曾经与现在
转载自:码农翻身 转自: 刘欣 码农翻身 1周前 我是Servlet, 由于很多框架把我深深地隐藏了起来,我变得似乎无关紧要了,很多人也选择性的把我给遗忘了. 其实,我还活得好好的呢, 只不过是从前台 ...
- APP界面架构设计
作为PM,信息架构和页面流的设计想必烂熟于心,当确定好产品战略层和范围层即为何种目标用户提供何种服务后,就要着手搭建功能架构,将目标功能通过良好的用户体验传递给用户,目的是高效解决用户痛点,从而实现价 ...