将两个验证码的视图都放到common蓝图下
from flask import Blueprint, request, make_responsefrom exts import alidayufrom utils import restfulfrom utils.captcha import Captchafrom .forms import SMSCaptchaFormfrom utils import cmscachefrom io import BytesIObp = Blueprint("common", __name__, url_prefix='/common')@bp.route('/captcha/')def graph_captcha(): """ 获取验证码 """ text, image = Captcha.gene_graph_captcha() cmscache.set(text.lower(), text.lower()) # key和value都是验证码,存到memcached中 out = BytesIO() # 二进制流 image.save(out, 'png') out.seek(0) resp = make_response(out.read()) # 二进制流读取出来放到响应对象中 resp.content_type = 'image/png' return resp@bp.route('/sms_captcha/', methods=['POST'])def sms_captcha(): """ 短信验证码 """ form = SMSCaptchaForm(request.form) if form.validate(): telephone = form.telephone.data captcha = Captcha.gene_text(number=4) if alidayu.send_sms(telephone, code=captcha): cmscache.set(telephone, captcha) # 把验证码存到memcached中,telephone:captcha return restful.success() else: # return restful.params_error('验证码发送失败') cmscache.set(telephone, captcha) # 把验证码存到memcached中,telephone:captcha return restful.success(captcha) # 由于没有触发发送验证码,这里只要手机号验证通过统一返回验证码 else: return restful.params_error('参数错误')
将模板的图形验证码的地址换为commo.graph_captcha