Flask 入门3:Flask 请求上下文与请求

1. 前言

Flask 在处理请求与响应的过程:

首先我们从浏览器发送一个请求到服务端,由 Flask 接收了这个请求以后,这个请求将会由路由系统接收。然后在路由系统中,还可以挂入一些 "勾子",在进入我们的 viewFunction 以前做一些预处理。即产生一个 request 对象,可以获取一些变量、cookie等参数,再传入 viewFunction 里面做一个进一步的处理。处理完了以后,一般上不需要直接去产生一个 response,而是通过 render_template 传给 Flask。

2. login

login.html:

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>方法:{{ method }}</h1>
<form method="post">
    <div>
        <input type="text"
               name="username"
               placeholder="User name" />
    </div>
    <div>
        <input type="text"
               name="password"
               placeholder="Password" />
    </div>
    <input type="submit">
</form>
</body>
</html>
python 复制代码
@app.route('/login', methods=['GET', 'POST'])
def login():
    if request.method == 'POST':
        username = request.form['username']
        password = request.form['password']
    else:
        username = request.args['username']

    return render_template('login.html',method=request.method)

可直接使用浏览器传值:

3. 上传文件

python 复制代码
@app.route('/upload', methods=['GET', 'POST'])
def uploads():
    if request.method == 'POST':
        f = request.files['file']
        basepath = path.abspath(path.dirname(__file__))  # 获取当前绝对路径
        upload_path = path.join(basepath, 'static/uploads')
        f.save(upload_path, secure_filename(f.filename))

        return redirect(url_for('uploads'))
    return render_template('upload.html')

upload.html:

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Upload new File</title>
</head>
<body>
<h1>文件上传示例</h1>
<form action=""
      method="post"
      enctype="multipart/form-data">
  <p>
    <input type="file"
           name="file">
    <input type="submit"
           value="Upload">
  </p>
</form>
</body>
</html>

4. errorhandler

对错误进行监控:

python 复制代码
@app.errorhandler(404)
def page_not_found(error):
    return render_template('404.html'), 404


@app.errorhandler(400)
def bad_request(error):
    return render_template('400.html'), 400

404.html:

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
<h1>404</h1>
<h2>很抱歉!</h2>
<p>您访问的页面并不存在</p>
</body>
</html>

400.html:

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
<h1>400</h1>
<h2>很抱歉!</h2>
<p>您发送了一个错误的请求</p>
</body>
</html>
相关推荐
逄逄不是胖胖31 分钟前
《动手学深度学习》-60translate实现
人工智能·python·深度学习
橘颂TA31 分钟前
【测试】自动化测试函数介绍——web 测试
python·功能测试·selenium·测试工具·dubbo
爱学习的阿磊35 分钟前
Python上下文管理器(with语句)的原理与实践
jvm·数据库·python
m0_7369191039 分钟前
Python面向对象编程(OOP)终极指南
jvm·数据库·python
one____dream42 分钟前
【网安】Reverse-非常规题目
linux·python·安全·网络安全·ctf
冷雨夜中漫步1 小时前
python反转列表reverse()和[::-1]哪个效率更高
开发语言·python
rainbow68891 小时前
Python面向对象编程与异常处理实战
开发语言·python
weixin199701080161 小时前
锦程物流item_get - 获取详情接口对接全攻略:从入门到精通
数据库·python
李梨同学丶1 小时前
0201好虫子周刊
后端
2501_907136821 小时前
基于Python+QT6的移动硬盘弹出工具
python·软件需求