第6节、扩展库的使用

binscor
2019-08-19 / 0 评论 / 540 阅读 / 正在检测是否收录...
温馨提示:
本文最后更新于2021年09月10日,已超过1167天没有更新,若内容或图片失效,请留言反馈。

一、flash消息的显示

概述: 当用户状态发生改变等等的处理时, 需要给用户显示出提示信息 flash就是实现这个功能的

导入: from flask import flash,get_flashed_messages

  • flash 存数据
  • get_flashed_messages() 获取flash存储的数据

实例

manage.py

#ajax处理登录的视图
@app.route('/do_login/',methods=['POST'])
def dologin():
    # print(request.args.get('username'))
    username = request.form.get('username')
    userpass = request.form.get('userpass')
    if username == 'zhangsan':
        if userpass == '123456':
            flash("登录成功")
            # flash("登录成功2")
            # flash("登录成功3")
            # print(get_flashed_messages())
            data = {'code':200}
        else:
            data = {'code':400}
    else:
        data = {'code':404}
    return jsonify(data)

base.html

{% for message in get_flashed_messages() %}
    <div class="alert alert-success alert-dismissible" role="alert">
    <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span
    aria-hidden="true">&times;</span></button>
    <strong>Warning!</strong> {{ message }}
    </div>SS
{% endfor %}

二、flask-moment 时间显示的扩展库

概述: 专门负责时间的显示 可以显示当前时间 可以计算时间的差值

附:用法

导入:from flask_moment import Moment

manage.py

from flask_moment import Moment #时间模块
from datetime import datetime,timedelta #导入时间模块
moment = Moment(app)
manager = Manager(app)

@app.route('/')
def index():
    nowtime = datetime.utcnow()+timedelta(seconds=-60) #前60秒的时间
    return render_template('index.html',nowtime=nowtime)

index.html

<h1>首页</h1>
    <ol>
        <li>{{ nowtime }}</li>
        <li>{{ moment(nowtime).format('L') }}</li>
        <li>{{ moment(nowtime).format('LL') }}</li>
        <li>{{ moment(nowtime).format('LLL') }}</li>
        <li>{{ moment(nowtime).format('LLLL') }}</li>
        <li>{{ moment(nowtime).format('YYYY-MM-DD') }}</li>
        <li>{{ moment(nowtime).fromNow() }}</li>
    </ol>

{% endblock %}
{% block scripts %}
    {{ super() }}
    {{ moment.include_moment() }} #导入moment显示的js代码
    {{ moment.locale('zh-CN') }} #格式化中国的时间
{% endblock %}

三、文件上传

1.原生文件上传

(1)简单上传

manage.py

@ys.route('/form/',methods=['GET','POST'])
def form():
    # if request.method == 'POST' and request.files.get('photo'):
    if request.method == 'POST' and 'photo' in request.files:
        # print(request.files)
        #获取传递过来的文件
        file = request.files.get('photo')
        #获取文件名称
        filename = file.filename
        #存储上传的路径和名字
        file.save(os.path.join(os.getcwd(),'static/upload/'+filename))
        return 'ok'
    return render_template('ysfile/form.html')

form.html

<form action="" method="post" enctype="multipart/form-data">
    <p>文件上传</p>
    <p><input type="file" name="photo"></p>
    <p><input type="submit" value="submit"></p>
</form>

附:python生成随机图形验证码

(2)添加条件判断的上传

manage.py

from flask import Blueprint,render_template,request,current_app
import string,random,os
#pip install pillow 处理图片
from PIL import Image

ys = Blueprint('ys',__name__)

#设置允许上传的文件类型
ALLOWED_FILE = ['jpg','jpeg','gif','png']

"""
文件上传的判断条件 有哪些?
1.类型
2.大小
3.处理文件名的生成
    获取后缀的方法?
4.上传
5.获取到名称 在模板中展示出来
"""
#生成随机文件名字的函数
def random_name(suffix,length=64):
    #生成大小写字母和数字的字符串
    Str = string.ascii_letters+string.digits
    #循环生成64位长度数字和字母的字符串的随机数+后缀
    return ''.join(random.choice(Str) for i in range(length))+'.'+suffix

#图片缩放函数
def img_zoom(path,width=200,height=200,prefix='s_'):
    img = Image.open(path)
    print(img.size)  # 获取图片的宽 高
    img.thumbnail((width, height))  # 重新设计尺寸
    pathSplit = os.path.split(path) # 将路径拆分成 路径和文件名
    newPath = os.path.join(pathSplit[0],prefix+pathSplit[1]) #将前缀进行拼接
    img.save(newPath)  # 保存图片 覆盖掉原来的图片

@ys.route('/form/',methods=['GET','POST'])
def form():
    img_name = None
    if request.method == 'POST' and 'photo' in request.files:
        file = request.files.get('photo') #拿到上传文件
        suffix = file.filename.split('.')[-1] #获取后缀
        if suffix in ALLOWED_FILE: #判断是否允许的文件类型
            #确保名字唯一性
            while True:
                newName = random_name(suffix) #生成随机名字
                path = os.path.join(current_app.config['UPLOADED_FOLDER'], newName)
                if not os.path.exists(path):
                    break
            #保存
            file.save(path)  #将名字传递给模板进行展示
            #进行图片的缩放
            img_zoom(path)
            img_name = newName
    return render_template('ysfile/form.html',img_name=img_name)

form.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
{#<ol>
    <li><h3>当文件上传失败 查看几点错误原因</h3></li>
    <li>methods是否为post</li>
    <li>input type=file 是否有name名称</li>
    <li>是否更改了enctype的值</li>
</ol>#}
{% if img_name %}
{#    <img src="{{ url_for('static',filename='upload/'+img_name) }}" alt="">#}
    <img src="{{ img_name }}" alt="">
{% endif %}
<form action="" method="post" enctype="multipart/form-data">
    <p>文件上传</p>
    <p><input type="file" name="photo"></p>
    <p><input type="submit" value="submit"></p>
</form>
</body>
</html>

2.使用flask-uploads进行文件上传

概述: 在文件上传时,提供了文件过滤和校验的功能

导入: from flask_uploads import IMAGES,UploadSet,configure_uploads,patch_request_class

manage.py

from flask import Flask,request,render_template
from flask_script import Manager
from flask_bootstrap import Bootstrap
import os
from flask_uploads import IMAGES,UploadSet,configure_uploads,patch_request_class

app = Flask(__name__)
#设置上传文件的大小 配置 写死的
app.config['MAX_CONTENT_LENGTH'] = 1024*1024*64 #64兆
app.config['UPLOADED_FOLDER'] = os.path.join(os.getcwd(),'static/upload')
#flask-uploads上传的时候 自动查找存储文件的目录
app.config['UPLOADED_PHOTOS_DEST'] = os.path.join(os.getcwd(),'static/upload')
bootstrap = Bootstrap(app)

file = UploadSet('photos',IMAGES) #配置文件上传
configure_uploads(app,file) #将app和配置关联到一起
patch_request_class(app,size=None) #使上传文件大小从 app.config['MAX_CONTENT_LENGTH'] 进行获取
manager = Manager(app)


@app.route('/')
def index():
    return 'index'

@app.route('/wtfrom/',methods=['GET','POST'])
def wtfrom():
    img_name = None
    if request.method == 'POST' and 'photo' in request.files:
        filename = file.save(request.files.get('photo'))
        print(filename)
        img_name = file.url(filename=filename)
        print(img_name)
    return render_template('ysfile/form.html',img_name=img_name)


from yuanshengfile import ys
# from flask_wtf_form import fwf
app.register_blueprint(ys) #注册原生文件上传的蓝本
# app.register_blueprint(fwf) #注册flask-wtf文件上传的蓝本
if  __name__ == '__main__':
    manager.run()

form.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
{#<ol>
    <li><h3>当文件上传失败 查看几点错误原因</h3></li>
    <li>methods是否为post</li>
    <li>input type=file 是否有name名称</li>
    <li>是否更改了enctype的值</li>
</ol>#}
{% if img_name %}
{#    <img src="{{ url_for('static',filename='upload/'+img_name) }}" alt="">#}
    <img src="{{ img_name }}" alt="">
{% endif %}
<form action="" method="post" enctype="multipart/form-data">
    <p>文件上传</p>
    <p><input type="file" name="photo"></p>
    <p><input type="submit" value="submit"></p>
</form>
</body>
</html>

四、flask-wtf flask-uploads flask-bootstrap 表单

manage.py

from flask import Flask,render_template
from flask_bootstrap import Bootstrap
from flask_script import Manager
from flask_wtf import FlaskForm
from flask_wtf.file import FileField,FileRequired,FileAllowed #导入自定义文件上传的表单 所需要的字段和验证器
from wtforms import SubmitField
from flask_uploads import UploadSet,IMAGES,patch_request_class,configure_uploads #文件上传扩展库 所需要的
import os,random,string
from PIL import Image



app = Flask(__name__)
app.config['SECRET_KEY'] = 'flask-wtf' #密钥  值随便
app.config['BOOTSTRAP_SERVE_LOCAL'] = True #加载本地bootstrap样式
app.config['MAX_CONTENT_LENGTH'] = 1024*1024*64 #设置上传文件大小
app.config['UPLOADED_PHOTOS_DEST'] = os.path.join(os.getcwd(),'static/upload')
#设置上传文件的保存目录
file = UploadSet('photos',IMAGES) #上传文件类型允许的设置
configure_uploads(app,file) #将配置和flask绑定
patch_request_class(app,size=None) #设置上传大小 获取MAX——CONTENT——LENGTH的大小
bootstrap = Bootstrap(app)
manager = Manager(app)

#配置上传表单
class FileUpload(FlaskForm):
    photos = FileField('上传头像',validators=[FileRequired(message='文件不能为空'),FileAllowed(file,message='只能上传图片')])
    submit = SubmitField('上传')

#随机名字
def random_name(suffix,length=32):
    Str = string.ascii_letters+string.digits
    return ''.join(random.choice(Str) for i in range(length))+'.'+suffix


#图片缩放函数
def img_zoom(path,width=200,height=200,prefix='s_'):
    img = Image.open(path)
    print(img.size)  # 获取图片的宽 高
    img.thumbnail((width, height))  # 重新设计尺寸
    pathSplit = os.path.split(path) # 将路径拆分成 路径和文件名
    newPath = os.path.join(pathSplit[0],prefix+pathSplit[1]) #将前缀进行拼接
    img.save(newPath)  # 保存图片 覆盖掉原来的图片



@app.route('/form/',methods=['GET','POST'])
def upload():
    img_url = None
    form = FileUpload()
    if form.validate_on_submit():
        photo = form.photos.data #request.files.get('photos')
        suffix = photo.filename.split('.')[-1] #取出后缀
        while True: #确保生成的名字唯一性
            filename = random_name(suffix)
            path = os.path.join(app.config['UPLOADED_PHOTOS_DEST'],filename)
            if not os.path.exists(path):
                break
        #保存图片  并使用生成的随机名字
        file.save(photo,name=filename)
        #进行图片的缩放
        img_zoom(path)
        #进行图片的缩放
        img_zoom(path,300,300,'m_')
        #获取上传图片的url地址
        img_url = file.url(filename)
    return render_template('form.html',form=form,img_url=img_url)
if __name__ == '__main__':
    manager.run()

form.html

{% extends 'bootstrap/base.html' %}
{% block title %}
    最终版 文件上传
{% endblock %}
{% from 'bootstrap/wtf.html' import quick_form %}
{% block content %}
    <div class="container">
        {% if img_url %}
            <img src="{{ img_url }}" style="border-radius: 400px;" alt="">
        {% endif %}
        <h2>文件上传</h2>
        {{ quick_form(form) }}
    </div>
{% endblock %}

五、设置临时的环境变量

ubuntu:

设置 export 名=值

获取 echo $名

windwos:

设置 set 名=值

获取 set名

六、发送邮件(flask-mail)

manage.py

from flask import Flask,render_template
from flask_bootstrap import Bootstrap
from flask_script import Manager
from flask_mail import Mail,Message
import os,time

app = Flask(__name__)
app.config['MAIL_SERVER'] = os.environ.get('MAIL_SERVER','smtp.1000phone.com')
app.config['MAIL_USERNAME'] = os.environ.get('MAIL_USERNAME','xialigang@1000phone.com')
app.config['MAIL_PASSWORD'] = os.environ.get('MAIL_PASSWORD','123456')
bootstrap = Bootstrap(app)
manager = Manager(app)

#发送邮件对象
mail = Mail(app)


@app.route('/send_mail/')
def send_mail():
    msg = Message(subject='账户激活',recipients=['948807313@qq.com'],sender=app.config['MAIL_USERNAME'])
    msg.html = render_template('send_mail.html')
    mail.send(message=msg)
    # time.sleep(2)
    return '发送邮件'


if __name__ == '__main__':
    manager.run()

异步发送 开启线程

from flask import Flask,render_template
from flask_bootstrap import Bootstrap
from flask_script import Manager
from flask_mail import Mail,Message
import os,time
from threading import Thread #导入线程模块


app = Flask(__name__)
app.config['MAIL_SERVER'] = os.environ.get('MAIL_SERVER','smtp.1000phone.com')
app.config['MAIL_USERNAME'] = os.environ.get('MAIL_USERNAME','xialigang@1000phone.com')
app.config['MAIL_PASSWORD'] = os.environ.get('MAIL_PASSWORD','123456')
bootstrap = Bootstrap(app)
manager = Manager(app)

#发送邮件对象
mail = Mail(app)

def send_mail_async(msg):
    #管理程序上下文
    with app.app_context():
        mail.send(message=msg)


@app.route('/send_mail/')
def send_mail():
    msg = Message(subject='账户激活',recipients=['948807313@qq.com'],sender=app.config['MAIL_USERNAME'])
    msg.html = render_template('send_mail.html')
    thr = Thread(target=send_mail_async,args=(msg,))
    thr.start() #开启线程
    # time.sleep(2)
    return '发送邮件'


if __name__ == '__main__':
    manager.run()
0

评论 (0)

取消