Flask库学习
Created At :
Count:1.3k
Views 👀 :
Flask是一个非常小的PythonWeb框架,被称为微型框架;只提供了一个稳健的核心,其他功能全部是通过扩展实现的;意思就是我们可以根据项目的需要量身定制,也意味着我们需要学习各种扩展库的使用。
基本概念
创建
通过创建路由并关联函数,实现一个基本的网页
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| from flask import Flask
app = Flask(__name__)
@app.route("/")
def index(): return "Hello World!"
app.run()
|
- **@app.route(‘/‘)**: 这是一个装饰器,用于告诉 Flask 哪个 URL 应该触发下面的函数。在这个例子中,它指定了根 URL(即网站的主页)。
也可以@app.route(‘/admin’)/@app.route(‘/admin/admin2’)…
传参
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
路由规则支持不同类型的参数和匹配规则。
类型规则:
字符串(默认): 匹配任意字符串。 整数(<int:name>): 匹配整数值。 浮点数(<float:value>): 匹配浮点数值。 路径(<path:name>): 匹配任意字符,包括斜杠 /。 @app.route('/blog/<name>') def blog(name): return f"hello {name}"
|
请求对象
请求对象包含了客户端发送的请求信息,包括请求方法、URL、请求头、表单数据等。Flask 提供了 request 对象来访问这些信息。
1 2 3 4 5 6
| from flask import request
@app.route('/submit', methods=['POST']) def submit(): username = request.form.get('username') return f'Hello, {username}!'
|
request.form.get(‘username’):获取 POST 请求中表单数据的 username 字段。
响应对象
响应对象包含了发送给客户端的响应信息,包括状态码、响应头和响应体。Flask 默认会将字符串、HTML 直接作为响应体。
1 2 3 4 5 6 7
| from flask import make_response
@app.route('/custom_response') def custom_response(): response = make_response('This is a custom response!') response.headers['X-Custom-Header'] = 'Value' return response
|
make_response:创建一个自定义响应对象,并设置响应头 X-Custom-Header。
模板
普通传参
Flask 使用 Jinja2 模板引擎来渲染 HTML 模板。模板允许你将 Python 代码嵌入到 HTML 中,从而动态生成网页内容。
这些html文件都在templates文件夹中
1 2 3 4 5 6
| from flask import render_template //注意导入render_template
@app.route('/hello/<name>') def hello(name): return render_template('hello.html', name=name)//html文件也是可以传参数的
|
模板文件hello.html
//模板文件接受py文件传入的参数需用{{}}包裹
1 2 3 4 5 6 7 8 9
| <!DOCTYPE html> <html> <head> <title>Hello</title> </head> <body> <h1>Hello, {{ name }}!</h1> </body> </html>
|
类与字典的传参
py文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| from flask import Flask,render_template app = Flask(__name__) /类 class student: def __init__(self,name,age): self.name = name self.age = age @app.route("/") def index(): /字典 person = { "x": "xxx", "y": "yyy" } stu = student(name="fmx",age="18") return render_template("moban_3.html",stu=stu,person=person) if __name__=="__main__": app.run(debug=True)
|
moban_3文件
1 2 3 4 5 6 7 8 9 10 11
| <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>fmx</title> </head> <body> <h1>你的姓名是{{stu.name}},年龄是{{stu.age}}</h1> <h1>字典可以这样{{person["x"]}},也可以这样{{person.y}}</h1> </body> </html>
|
模板继承
模板继承允许你创建一个基础模板,然后在其他模板中继承和扩展这个基础模板,避免重复的 HTML 代码。
创建基础模板:
在 templates 文件夹中创建一个基础模板 base.html。
templates/base.html 示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| <!DOCTYPE html> <html> <head> <title>{% block title %}My Website{% endblock %}</title> </head> <body> <header> <h1>My Website</h1> </header> <main> {% block content %}{% endblock %} </main> <footer> <p>Footer content</p> </footer> </body> </html>
|
和 是定义的可替换区域。
创建子模板:在 templates 文件夹中创建一个子模板 index.html,继承 base.html。
templates/index.html 文件代码:
1 2 3 4 5 6 7 8
| #{% extends "base.html" %} //开头继承
#{% block title %}Home Page{% endblock %} //自定义区域
{% block content %} <h2>Welcome to the Home Page!</h2> <p>Content goes here.</p> {% endblock %}
|
**<!–swig32–>**:继承基础模板。
**
转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。