cnblogs 系统
图片验证码生成
from PIL import Image,ImageDraw,ImageFont import random from io import BytesIO from django.http import JsonResponse def get_valid_img(request): def get_random_color(): """ 生成随机颜色 """ return (random.randint(0,255),random.randint(0,255),random.randint(0,255)) def get_random_char(): """ 生成一个随机数字或字母 """ random_num=str(random.randint(0,9)) random_upper_alph=chr(random.randint(65,90)) random_lowwer_alph=chr(random.randint(97,122)) random_char=random.choice([random_num,random_lowwer_alph,random_upper_alph]) return random_char image = Image.new(mode="RGB",size=(200,50),color=get_random_color()) #生成画板 draw = ImageDraw.Draw(image,mode="RGB") #生成画笔 font=ImageFont.truetype("appblog/static/kumo.ttf",32) #文字样式 valid_code_str="" for i in range(1,6): #生成五位随机验证码 char=get_random_char() valid_code_str+=char draw.text([i*30,5],char,get_random_color(),font=font) #在画板上写字 width=200 height=50 # for i in range(80): #画点 # draw.point((random.randint(0,width),random.randint(0,height)),fill=get_random_color()) # # for i in range(5): #画线 # x1=random.randint(0,width) # x2=random.randint(0,width) # y1=random.randint(0,height) # y2=random.randint(0,height) # draw.line((x1,y1,x2,y2),fill=get_random_color()) # for i in range(40): #画圆 # draw.point([random.randint(0, width), random.randint(0, height)], fill=get_random_color()) # x = random.randint(0, width) # y = random.randint(0, height) # draw.arc((x, y, x + 4, y + 4), 0, 90, fill=get_random_color()) f = BytesIO() image.save(f,"png") #图片保存在内存 data = f.getvalue() #拿到内存中图片的数据 print(valid_code_str) #验证码 request.session["valid_code_str"] = valid_code_str #写session return HttpResponse(data)
头像预览
//头像预览 $("#id_avatar").change(function () { var file = $("#id_avatar")[0].files[0]; //实例化一个阅读器 var reader = new FileReader(); reader.readAsDataURL(file); //绑定读完做的事件 reader.onload = function () { $("#avatar_img").attr("src",this.result) } });
ajax实现form表单验证码比对(类继承)
视图
form类
用法
media 配置
#用户上传文件存放地址
MEDIA_ROOT = os.path.join(BASE_DIR,"appblog","media")
# 在浏览器能访问到MEDIA_ROOT的文件
MEDIA_URL = '/media/'
# 在urls中添加 让客户端能访问到media下的文件
from django.views.static import serve
from . import settings
url(r'^media/(?P<path>.*)$', serve, {'document_root': settings.MEDIA_ROOT}),
admin 配置
配置urls
extra 查询应用
ORM事件回滚
输入框获得焦点事件 : $.focus()
location.seach :获得url里的数据部分
url中?后面的所有数据
slice() 是字符串分割方法,参数是索引
print(location.seach) # next=/xxxx/xxx/xx
富文本编辑器 kindeditor
官网:http://kindeditor.net/demo.php
文档:http://kindeditor.net/docs/index.html
编辑器初始化参数:http://kindeditor.net/docs/option.html
上传图片视图与返回值
注意事项:
必须用<textarea name="content" id="editor_id" cols="30" rows="10"></textarea>标签
id 值必须唯一
滑动验证码
在form表单中添加一个div标签
<div id="popup-captcha"></div>
登陆页面脚本
{#滑动验证码#} <script> var handlerPopup = function (captchaObj) { // 成功的回调 captchaObj.onSuccess(function () { var validate = captchaObj.getValidate(); $.ajax({ url: "/pc-geetest/ajax_validate", // 进行二次验证 type: "post", dataType: "json", data: { username: $("#inputuser").val(), password: $("#inputPassword").val(), csrfmiddlewaretoken: $("[name='csrfmiddlewaretoken']").val(), geetest_challenge: validate.geetest_challenge, geetest_validate: validate.geetest_validate, geetest_seccode: validate.geetest_seccode }, success: function (data) { if (data.user) { console.log("登录成功"); if (location.search.slice(6)){ location.href = location.search.slice(6) }else { location.href = "/index/" } } else { $("#error").text(data.error_msg); setTimeout(function () { $("#error").text("") }, 3000) } } }); }); $("#popup-submit").click(function () { captchaObj.show(); }); // 将验证码加到id为captcha的元素里 captchaObj.appendTo("#popup-captcha"); // 更多接口参考:http://www.geetest.com/install/sections/idx-client-sdk.html }; // 验证开始需要向网站主后台获取id,challenge,success(是否启用failback) $.ajax({ url: "/pc-geetest/register?t=" + (new Date()).getTime(), // 加随机数防止缓存 type: "get", dataType: "json", success: function (data) { // 使用initGeetest接口 // 参数1:配置参数 // 参数2:回调,回调的第一个参数验证码对象,之后可以使用它做appendTo之类的事件 initGeetest({ gt: data.gt, challenge: data.challenge, product: "popup", // 产品形式,包括:float,embed,popup。注意只对PC版验证码有效 offline: !data.success // 表示用户后台检测极验服务器是否宕机,一般不需要关注 // 更多配置参数请参见:http://www.geetest.com/install/sections/idx-client-sdk.html#config }, handlerPopup); } }); </script>
配置url
from appblog.views import pcgetcaptcha from appblog.views import pcajax_validate #滑动验证码 url(r'^pc-geetest/register', pcgetcaptcha, name='pcgetcaptcha'), url(r'^pc-geetest/ajax_validate', pcajax_validate, name='pcajax_validate'),
视图函数
#################滑动验证 import json from django.shortcuts import render from django.http import HttpResponse from appblog.geetest import GeetestLib pc_geetest_id = "b46d1900d0a894591916ea94ea91bd2c" pc_geetest_key = "36fc3fe98530eea08dfc6ce76e3d24c4" def pcgetcaptcha(request): user_id = 'test' gt = GeetestLib(pc_geetest_id, pc_geetest_key) status = gt.pre_process(user_id) request.session[gt.GT_STATUS_SESSION_KEY] = status request.session["user_id"] = user_id response_str = gt.get_response_str() return HttpResponse(response_str) # 登录验证 def pcajax_validate(request): if request.method == "POST": gt = GeetestLib(pc_geetest_id, pc_geetest_key) challenge = request.POST.get(gt.FN_CHALLENGE, '') validate = request.POST.get(gt.FN_VALIDATE, '') seccode = request.POST.get(gt.FN_SECCODE, '') status = request.session[gt.GT_STATUS_SESSION_KEY] user_id = request.session["user_id"] response={"user":None,"error_msg":""} if status: result = gt.success_validate(challenge, validate, seccode, user_id) else: result = gt.failback_validate(challenge, validate, seccode) user=request.POST.get("username") pwd=request.POST.get("password") #通过验证result为1,否则为0 if result: user = auth.authenticate(username=user, password=pwd) if user: auth.login(request, user) # 设置session response["user"] = user.username else: response["error_msg"] = "账户或密码不正确" else: response["error_msg"]="验证码错误!" return HttpResponse(json.dumps(response)) return HttpResponse("error")
下载文件
BeautifulSoup
导入
from bs4 import BeautifulSoup
文章摘要处理
获得所有标签文本(去换行)用索引截取
参考博客: http://www.cnblogs.com/yuanchenqi/articles/7617280.html#_label0
既然选择了远方,便是风雨兼程...