Flask学习_3 _消息提示与异常处理

from flask import Flask, render_template, flash, request, abort

app = Flask(__name__)
app.secret_key = '345'
# Flask会使用secret_key对消息进行加密
'''
消息提示
抛出异常
异常处理

flask提供了消息闪现机制
'''

@app.route('/')
def hello_world():
    flash('消息闪现')
    return render_template('index_flash.html')


@app.route('/login', methods=['POST'])
def login():
    form = request.form
    username = form.get('username')
    password = form.get('password')
    print(type(username))
    print(type(password))

    if not username:
        flash('请输入用户名')
        return render_template('index_flash.html')

    if not password:
        flash('请输入密码')
        return render_template('index_flash.html')

    if str(username) == '123' and str(password) == '123':
        flash('登陆成功')
        return render_template('index_flash.html')
    else:
        flash('username or password id wrong')
        return render_template('index_flash.html')


@app.errorhandler(404)
def not_found(e):  # 此处有个参数e,不然会报错
    return render_template('404.html')


@app.route('/users/<user_id>')
def users(user_id):
    if int(user_id) == 1:
        return render_template('user.html')
    else:
        abort(404)  # 这里直接抛出404异常


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

简单了解了下能从request中获得什么东西

request.method # 请求方式
request.form # 存放FormData中的数据 to_dict 序列化成字典
request.args # 获取URL中的数据 to_dict 序列化成字典
request.url # 访问的完整路径
request.path # 路由地址
request.host # 主机地址
request.values # 获取 FormData and URL中的数据 不要用to_dict
request.json  # 如果提交时请求头中的Content-Type:application/json 字典操作
request.data  # 如果提交时请求头中的Content-Type 无法被识别 将请求体中的原始数据存放 byte
request.cookies # 获取Cookie中的数据
request.headers # 获取请求头
request.files # 序列化文件存储 save()

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值