青海商城网站建设,公司取名字,17模板网站,wordpress自定义字段类型一、理论 Flask是一个轻量级的web框架#xff0c;灵活易用。提供构建web应用所需的核心工具。
Flask依赖python的两个库 Werkzeug#xff1a;flask的底层库#xff0c;提供了WSGI接口、HTTP请求和响应处理、路由等核心功能。 Jinja2#xff1a;模板引擎#xff0…一、理论 Flask是一个轻量级的web框架灵活易用。提供构建web应用所需的核心工具。
Flask依赖python的两个库 Werkzeugflask的底层库提供了WSGI接口、HTTP请求和响应处理、路由等核心功能。 Jinja2模板引擎用于动态生成HTML页面。 二、实践
1、第一个flask应用
[rootlocalhost ~]# pip config set global.index-url http://mirrors.aliyun.com/pypi/simple
Writing to /root/.config/pip/pip.conf
[rootlocalhost ~]# pip3 config set global.index-url http://mirrors.aliyun.com/pypi/simple
Writing to /root/.config/pip/pip.conf
[rootlocalhost ~]# pip3 config set install.trusted-host mirrors.aliyun.com
Writing to /root/.config/pip/pip.conf
[rootlocalhost ~]# pip3 install --upgrade pip[rootlocalhost ~]# pip install flask[rootlocalhost ~]# vim a.py
from flask import Flask# 创建Flask应用实例
appFlask(__name__)# 定义路由和视图函数
app.route(/) # 用户的访问url为根时flask会调用hello_world()函数。
def hello_world():return Hello,World!# 启动应用
if __name__ __main__: # 该行用于确认当前脚本是否是通过命令行直接运行的而不是作为其他模块或程序的一部分被导入的。app.run(host0.0.0.0,port5000,debugTrue) # 启动flask开发服务器debugtrue表示启用调试模式可在开发过程中自动重载应用并在发生错误时显示详细信息。 host指定监听的地址这里监听本机所有可用的地址port指定监听的端口。[rootlocalhost ~]# python3 a.py # 运行该服务。* Serving Flask app a* Debug mode: on
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.* Running on all addresses (0.0.0.0)* Running on http://127.0.0.1:5000* Running on http://192.168.10.101:5000
Press CTRLC to quit* Restarting with stat* Debugger is active!* Debugger PIN: 112-870-242[rootlocalhost ~]# firewall-cmd --add-port5000/tcp # 开放端口
success [rootlocalhost ~]# curl 192.168.10.101:5000
Hello,World![rootlocalhost ~]# # 这里会这样显示是因为return的末尾未加\n换行所以导致这样显示。命令行中输入完命令在末尾默认会加上换行所以不会出现这种情况。修改代码from flask import Flask# 创建Flask应用实例
appFlask(__name__)# 定义路由和视图函数
app.route(/)
def hello_world():return Hello,World!\n # 加上换行符即可。# 启动应用
if __name__ __main__:app.run(host0.0.0.0,port5000,debugTrue)[rootlocalhost ~]# curl 192.168.10.101:5000
Hello,World!
[rootlocalhost ~]# c^C[rootlocalhost ~]# vim a.py
[rootlocalhost ~]# python3 a.py* Serving Flask app a* Debug mode: on
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.* Running on all addresses (0.0.0.0)* Running on http://127.0.0.1:5000* Running on http://192.168.10.101:5000
Press CTRLC to quit* Restarting with stat* Debugger is active!* Debugger PIN: 112-870-242
192.168.10.101 - - [21/Apr/2025 09:08:50] GET / HTTP/1.1 200 -
192.168.10.101 - - [21/Apr/2025 09:08:53] GET / HTTP/1.1 200 -2、flask路由与视图函数
flask通过装饰器app.route()来定义路由而视图函数则负责处理用户的请求并返回响应。[rootlocalhost ~]# vim a.py
from flask import Flask# 创建Flask应用实例
appFlask(__name__)# 定义路由和视图函数
app.route(/)
def hello_world():return Hello,World!\napp.route(/greet/name) # 这里定义了路径ip/greet/name 如果输入1参数1会传递到下面的return中会显示hello1.
def greet(name):return fHello,{name}!\n # f是用于字符串格式化将输出结果显示为字符串。# 启动应用
if __name__ __main__:app.run(host0.0.0.0,port5000,debugTrue)[rootlocalhost ~]# python3 a.py* Serving Flask app a* Debug mode: on
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.* Running on all addresses (0.0.0.0)* Running on http://127.0.0.1:5000* Running on http://192.168.10.101:5000
Press CTRLC to quit* Restarting with stat* Debugger is active!* Debugger PIN: 112-870-242
192.168.10.101 - - [21/Apr/2025 09:14:38] GET /greet/1 HTTP/1.1 200 -[rootlocalhost ~]# curl 192.168.10.101:5000/greet/1
Hello,1!注意这里只能这样写写为192.168.10.101/greet/1:5000会失败它会去找80端口。因为url的格式就是这样平时访问网页就是ip:port只不过port被隐藏了因为是80443常用端口不需要写。[rootlocalhost ~]# curl 192.168.10.101/greet/1:5000
curl: (7) Failed to connect to 192.168.10.101 port 80 after 0 ms: Couldnt connect to serverURL的组成部分协议protocol指定了资源应该使用的访问方式常见的协议有http、https、ftp等主机名hostname资源所在的服务器地址可以是ip地址或域名。端口号port服务器上用于访问资源的技术接口。路径path资源在服务器上的具体位置。参数parameters提供给服务器的额外信息通常以键值对的形式出现。查询query通过与url的其他部分分隔用于提供额外的请求信息。片段fragment通常以#开始指向资源内部的一个锚点如网页中的一个特定部分。url完整格式 http://www.hostname.com:80/index.html?langzh#contenthttp是协议www.hostname.com是主机名80是端口/index.html是路径langzh是查询参数content是片段标识符。3、指定允许的请求方法。
[rootlocalhost ~]# vim a.py
from flask import Flask# 创建Flask应用实例
appFlask(__name__)# 定义路由和视图函数
app.route(/)
def hello_world():return Hello,World!\napp.route(/greet/name)
def greet(name):return fHello,{name}!\napp.route(/submit,methods[POST])
def submit():return Form submitted successfully!\n # Form是html里定义请求方式时用到的关键字。# 启动应用
if __name__ __main__:app.run(host0.0.0.0,port5000,debugTrue) [rootlocalhost ~]# python3 a.py * Serving Flask app a* Debug mode: on
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.* Running on all addresses (0.0.0.0)* Running on http://127.0.0.1:5000* Running on http://192.168.10.101:5000
Press CTRLC to quit* Restarting with stat* Debugger is active!* Debugger PIN: 112-870-242[rootlocalhost ~]# curl 192.168.10.101:5000/submit
!doctype html
html langen
title405 Method Not Allowed/title
h1Method Not Allowed/h1
pThe method is not allowed for the requested URL./p
[rootlocalhost ~]# curl -XPOST 192.168.10.101:5000/submit
Form submitted successfully!4、使用jinja2模板渲染html[rootlocalhost ~]# mkdir templates
[rootlocalhost ~]# ls
anaconda-ks.cfg a.py templates # 模板文件与主程序a.py必须在同一级目录下否则会找不到名称也必须叫templates否则也会找不到。
[rootlocalhost ~]# cd templates/
[rootlocalhost templates]# vim greet.html
html langen
headmeta charsetUTF-8titleFlask Example/title
/head
bodyh1Hello,{{ name }}!h1 # {{ }} 这种格式是jinja2模板格式。name同样是参数传递ip/greet/name中的name会传递到这里。
/body
/html[rootlocalhost ~]# vim a.py
from flask import Flask
from flask import render_template# 创建Flask应用实例
appFlask(__name__)# 定义路由和视图函数
app.route(/)
def hello_world():return Hello,World!\napp.route(/submit,methods[POST])
def submit():return Form submitted successfully!\napp.route(/greet/name)
def greet(name):return render_template(greet.html,namename)# 启动应用
if __name__ __main__:[rootlocalhost ~]# python3 a.py* Serving Flask app a* Debug mode: on
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.* Running on all addresses (0.0.0.0)* Running on http://127.0.0.1:5000* Running on http://192.168.10.101:5000
Press CTRLC to quit* Restarting with stat* Debugger is active!* Debugger PIN: 112-870-242[rootlocalhost ~]# curl 192.168.10.101:5000/greet/aaa
html langen
head meta charsetUTF-8titleFlask Example/title
/head
bodyh1Hello,aaa!h1
/body4、模板继承与块[rootlocalhost ~]# ls
anaconda-ks.cfg a.py templates
[rootlocalhost ~]# cat a.py
from flask import Flask
from flask import render_template# 创建Flask应用实例
appFlask(__name__)# 定义路由和视图函数
app.route(/)
def hello_world():return Hello,World!\napp.route(/submit,methods[POST])
def submit():return Form submitted successfully!\napp.route(/index)
def index():return render_template(index.html)# 启动应用
if __name__ __main__:app.run(host0.0.0.0,port5000,debugTrue)[rootlocalhost ~]# cd templates/
[rootlocalhost templates]# ls
base.html greet.html index.html
[rootlocalhost templates]# cat base.html
html langen
headmeta charsetUTF-8title{% block title %}My Website{% endblock %}/title # 定义块开始与结束位置并定义其中的内容。子模板引用父模板就是通过块来引用的。
/head
bodyheaderh1Welcome to My Website/h1/headerdiv # div是html里的块级元素属于容器可包含标题、段落、表格等等。{% block content %}{% endblock %} # 定义块开始与结束content是内容。/divfooterpcopy; 2025 My Website/p # 页脚处的信息。/footer
/body
/html
[rootlocalhost templates]# cat index.html
{% extends base.html %}{% block title %}Home{% endblock %} # 网站标签处的显示信息。例如百度的百度一下你就知道。{% block content %}h2 Welcome to the homepage !/h2###
{% extends base.html %} 子模版继承了base.html模板。
{% block title %}Home {% endblock %} 覆盖父模板中的title块。
{% block content %} 定义页面的主要内容区域。[rootlocalhost ~]# python3 a.py* Serving Flask app a* Debug mode: on
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.* Running on all addresses (0.0.0.0)* Running on http://127.0.0.1:5000* Running on http://192.168.10.101:5000
Press CTRLC to quit* Restarting with stat* Debugger is active!* Debugger PIN: 112-870-242[rootlocalhost ~]# curl 192.168.10.101:5000/index
html langen
headmeta charsetUTF-8titleHome/title
/head
bodyheaderh1Welcome to My Website/h1/headerdivh2 Welcome to the homepage !/h2/divfooterpcopy; 2025 My Website/p/footer
/body