Flask学习记录

韩佳*

Flask, PyCharm

项目描述

1.通过函数访问html模板 2.搭建登录页面,并连接数据库,完成登录功能 3.配置加载静态文件,如img,css... 4.创建登录接口,并使用postman连接,使其返回json格式的数据 5.通过函数传递值到html页面上,并渲染出来 6.完成Flask邮件发送功能,扩展:尝试发送html格式的邮件内容

上传时间

2021.06.22

浏览人数

763人
韩佳*
天津市南开区
Hot:20126

1. 通过函数访问html模板

    要想要flask返回一个页面,只需要return render_template(“模板名”,**参数)即可. 比如对于demo1.html

<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>Hello {{ name }}!</h1>
</body>
</html>

    可通过index函数渲染并返回

@app.route('/index')
def index():
    return render_template('demo1.html', **{'name': '乌龙奶茶'})


2. 搭建登录页面,并连接数据库,完成登录功能

    先创建一个可以完成登录功能的页面

Flask学习记录

<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
    <form action="/log" method="post">
        <span class="zh">账号:<input type="text" placeholder="请输入账号" name="name" required></span>
        <span class="mm">密码:<input type="password" placeholder="请输入密码" name="password" required></span>
        <div class="tip">
            <span style="color: red">{{ error_tip }}</span>
            <span style="color: green">{{ right_tip }}</span>
        </div>
        <button type="submit" class="but">登录</button>
    </form>
    </body>
</html>

    函数部分:

@app.route("/to_log")
def to_log():
    return render_template('login.html')


@app.route("/log", methods=['POST'])
def log():
    data = request.form
    n = data['name']
    pw = data['password']
    if User.query.filter(User.name == n).first():
        if User.query.filter(User.password == pw).first():
            return render_template('login.html', **{'right_tip': '登录成功'})
        else:
            return render_template('login.html', **{'error_tip': '密码错误,请重新输入'})
    else:
        return render_template('login.html', **{'error_tip': '用户不存在,请重新输入'})


3. 配置加载静态文件,如img,css...

<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<img src="/static/touxiang.jpg">
</body>
</html>

@app.route('/img')
def img():
    return render_template('img.html')


4. 创建登录接口,并使用postman连接,使其返回json格式的数据

    要想flask返回json格式数据,可以使用flask提供的jsonify方法格式化之后返回。

@app.route("/postman_log", methods=['POST'])
def postman_log():
    data = request.form
    n = data['name']
    pw = data['password']
    if n and pw:
        if User.query.filter(User.name == n).first():
            if User.query.filter(User.password == pw).first():
                result = {
                    "status": True,
                    "msg": "登录成功",
                    "data": {
                        "username": n,
                        'password': pw
                    }
                }
                return Response(json.dumps(result), mimetype='application/json')
            else:
                result = {
                    "status": False,
                    "msg": "密码错误,请重新输入",
                }
                return jsonify(result)
        else:
            result = {
                "status": False,
                "msg": "用户不存在,请重新输入",
            }
            return jsonify(result)
    else:
        result = {
            "status": False,
            "msg": "用户名和密码不能为空",
        }
        return jsonify(result)

Flask学习记录

Flask学习记录

Flask学习记录

Flask学习记录


5. 通过函数传递值到html页面上,并渲染出来 

@app.route('/milktea')
def data():
    mlist = MilkTea.query.all()
    print('mlist', mlist)
    for item in mlist:
        print(item.name)
    return render_template('milktea.html', **{'mlist': mlist})

<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
{% for m in mlist %}
    <p>name: {{ m.name }}
    price: {{ m.price }}
    des: {{ m.des }}</p>
{% endfor %}
</body>
</html>


6. Flask邮件发送功能,扩展:尝试发送html格式的邮件内容

    1 ) 首先使用pip安装flask-mail:

          pip install flask-mail 

    2 ) 配置信息

app.config.update(
    DEBUG=True,
    MAIL_SERVER='smtp.qq.com',  # 电子邮件服务器的主机名或IP地址
    MAIL_PROT=465,  # 电子邮件服务器的端口
    MAIL_USE_TLS=True,   # 启用传输层安全
    MAIL_USERNAME='**********@qq.com',  # 邮件账户用户名
    MAIL_PASSWORD='************',  # 授权码
)
mail = Mail(app)

          注意授权码不是邮箱密码!

          授权码获取方式: 登录网页邮箱 -> 设置 -> 账户

Flask学习记录

            根据提示发送短信获取授权码即可.

    3 ) 

@app.route('/email')
def email():
    # sender 发送方,recipients 接收方列表
    msg = Message("This is a test ", sender='1973707249@qq.com', recipients=['*********@qq.com'])
    # 邮件内容
    msg.body = "Flask test mail"
    msg.html = "<h3>这是一封邮件</h3>天津今天36度, 远比不上105度的你."
    # 发送邮件
    mail.send(msg)
    print("发送成功")
    return "发送成功"


if __name__ == "__main__":
    app.run()

Flask学习记录

韩佳*

当前作品暂无评分

还未获得评语哦~
python python培训 天津python培训 python培训班 python培训多少钱 Django 天津python培训班 天津python培训 天津python培训机构。天津python培训学什么? python python培训 天津python培训 Mysql django Numpy flask anaconda 天津python培训 天津python培训班 python培训 python 天津python培训 天津python培训班 数据爬虫 爬虫技术 python爬虫 python 天津python培训 python培训 python PyCharm Eclipse + 天津python培训 python培训 python PyCharm Eclipse + PyDev python培训 天津python培训 python培训班 python培训多少钱 PyCharm Eclipse + PyDev visual studio 天津python培训 天津python培训班 PyCharm Eclipse + PyDev visual studio 天津python培训 天津python培训班 天津python培训机构 python培训班 C/C++ Java Python 天津python培训 天津python培训班 天津python培训机构 python培训班 C/C++ Java Python 天津python培训 天津python培训班 天津python培训机构 python培训班 C/C++ Java Python 天津python培训 天津python培训班 天津python培训机构 python培训班 C/C++ Java Python 天津python培训 天津python培训班 天津python培训机构 python培训班 python java web 天津python培训 天津python培训哪家好 天津python培训机构 天津python培训班 python java web前端 天津python培训 天津python培训班 天津python培训机构 天津python培训学校 python java web 天津python培训 天津python培训班 天津python培训机构 python培训 python java web 天津python培训 天津python培训班 天津python培训机构 天津python培训学校 python java web 天津python培训 天津python培训班 天津python培训机构 python java web 天津python培训 天津python培训班 天津python培训机构 天津python培训学校 python Java web 天津python培训 天津python培训学校 天津python培训机构 天津python培训班 python java web 天津python培训 天津python培训班 天津python培训机构 python培训 python Java web 天津python培训 天津python培训机构 天津python培训班 python培训 python java web 天津python培训 天津python培训机构 天津python培训学校 python培训 python java web 天津python培训 天津python培训班 天津python培训机构 python培训 python java web 天津python培训 天津python培训班 天津python培训机构 python培训 python java web 天津python培训 天津python培训班 天津python培训机构 python培训学校 python java web 天津python培训 天津python培训机构 python培训班 python java web 天津Java培训 天津Java培训班 Java培训 天津Java培训机构 python web java 天津python培训 天津python培训班 天津python培训机构 python java web 天津python培训 天津python培训班 python培训 天津python培训机构 python java web 天津python培训 天津python培训班 天津python培训机构 python培训 python web java 天津python培训 天津python培训班 python培训 python web java 天津python培训 天津python培训班 python培训 python web java 天津python培训 天津python培训机构 天津python培训学校 python web java 天津python培训班 天津python培训机构 天津python培训学校 python java web 天津python培训 天津python培训机构 python培训 python web java 天津python培训 天津python培训班 python培训 python web java 天津python培训 天津python培训机构 python培训 python web java 天津python培训 天津python培训班 python培训 python web java 天津python培训 天津python培训班
韩佳*    20126 天津市南开区 设计师杨冰是女孩 1997.**.**
本网站已在中国版权保护中心登记了美术作品著作权与软件著作权违者将依法追究责任,特此声明! | Copyright©2013-2022,zhuzuoji.com | 诚筑说培训学校(天津)有限公司内容支持 | 京ICP备17020986号-5