- 第 26 天 - 网络 Python
- 26.1网页版 Python
- 26.2Flask
- 文件夹结构
- 26.3设置你的项目目录
- 26.4创建路由
- 26.5创建模板
- 26.6Python脚本
- 26.7导航
- 26.8创建布局
- 提供静态文件
- 26.9部署
- 第 27 天 - Python 与 MongoDB
- 27.1MongoDB
- 27.2获取连接字符串(MongoDB URI)
- 27.3将 Flask 应用程序连接到 MongoDB 集群
- 27.3创建数据库和集合
- 27.4将许多文档插入到集合中
- 27.5MongoDB 查找
- 27.6用查询查找
- 27.7使用修饰符查找查询
- 27.8限制文件
- 27.9排序查找
- 27.10使用查询更新
- 27.11删除文档
- 27.12删除一个集合
- 第 28 天 - API
- 28.1应用程序编程接口(API)
- 28.2构建API
- 28.3HTTP(超文本传输协议)
- 28.4HTTP的结构
- 28.5初始请求行(状态行)
- 28.6初始响应线(状态线)
- 28.7标题字段
- 28.8消息体
- 28.9请求方法
- 第 29 天 - 构建 API
- 29.1API的结构
- 29.2使用 get 检索数据
- 让我们导入Flask
- 29.3通过 id 获取文档
- 29.4使用 POST 创建数据
- 29.4使用 PUT 更新
- 29.5使用 Delete 删除文档
- 第 30 天-结论
第 26 天 - 网络 Python 26.1网页版 Python挑战学习Python编程30天最终篇,要结束了,后面还会持续更新其他方面,关注我
你们的三连(点赞,收藏,评论)是我持续输出的动力,感谢。
在兴趣中学习,效益超乎想象,有趣的源码与学习经验,工具安装包,欢迎加我的微信:bobin1124,一起交流学习与分享。
Python 是一种通用编程语言,可用于许多地方。在本节中,我们将了解如何在 Web 上使用 Python。Python 网页框架作品很多。Django 和 Flask 是最受欢迎的。今天,我们将看到如何使用 Flask 进行 Web 开发。
26.2FlaskFlask 是一个用 Python 编写的 Web 开发框架。Flask 使用 Jinja2 模板引擎。Flask 也可以与其他现代前端库一起使用,例如 React。
如果您还没有安装 virtualenv 包,请先安装它。虚拟环境将允许将项目依赖项与本地机器依赖项隔离开来。
文件夹结构完成所有步骤后,您的项目文件结构应如下所示:
├── 配置文件
├── app.py
├──环境
│ ├── bin
├──需求.txt
├── 静态
│ └── css
│ └── main.css
└── 模板
├── about.html
├── home.html
├── layout.html
├── post.html
└── 结果.html
26.3设置你的项目目录
按照以下步骤开始使用 Flask。
步骤 1:使用以下命令安装 virtualenv。
pip 安装 virtualenv
第2步:
asabeneh@Asabeneh:~/Desktop$ mkdir python_for_web asabeneh@Asabeneh:~/Desktop$ cd python_for_web/ asabeneh@Asabeneh:~/Desktop/python_for_web$ virtualenv venv asabeneh@Asabeneh:~/Desktop/python_for_web$ source venv/bin/activate (env) asabeneh@Asabeneh:~/Desktop/python_for_web$ pip freeze (env) asabeneh@Asabeneh:~/Desktop/python_for_web$ pip install Flask (env) asabeneh@Asabeneh:~/Desktop/python_for_web$ pip freeze Click==7.0 Flask==1.1.1 itsdangerous==1.1.0 Jinja2==2.10.3 MarkupSafe==1.1.1 Werkzeug==0.16.0 (env) asabeneh@Asabeneh:~/Desktop/python_for_web$
我们创建了一个名为 python_for_web 的项目主管。在项目中,我们创建了一个虚拟环境venv,它可以是任何名称,但我更喜欢称之为venv。然后我们激活了虚拟环境。我们使用 pip freeze 检查项目目录中已安装的包。pip freeze 的结果是空的,因为一个包还没有安装。
现在,让我们在项目目录中创建 app.py 文件并编写以下代码。app.py 文件将是项目中的主文件。下面的代码有flask模块,os模块。
26.4创建路由回家路线。
#让我们
从 flask 导入 flask import Flask
import os #导入操作系统模块
app = Flask ( __name__ )
@app.route('/') # 这个装饰器创建 home 路由
def home ():
return 'Welcome'
@app.route('/about')
def about ():
return 'about us'
如果 __name__ == '__main__' :
#部署,我们使用ENVIRON
#使之成为生产和开发工作
port = int(os.environ.get("PORT", 5000))
app.run(debug=True, host='0.0.0.0', port=port)
要运行flask 应用程序,请在主flask 应用程序目录中编写python app.py。
运行python app.py 后,检查本地主机 5000。
让我们添加额外的路线。创建关于路由
#让我们
从 flask 导入 flask import Flask
import os #导入操作系统模块
app = Flask ( __name__ )
@app.route('/') # 这个装饰器创建 home 路由
def home ():
return 'Welcome'
@app.route('/about')
def about ():
return 'about us'
如果 __name__ == '__main__' :
#部署,我们使用ENVIRON
#使之成为生产和开发工作
port = int(os.environ.get("PORT", 5000))
app.run(debug=True, host='0.0.0.0', port=port)
现在,我们在上面的代码中添加了 about 路由。如果我们想渲染一个 HTML 文件而不是字符串呢?可以使用函数render_templae渲染 HTML 文件。让我们创建一个名为 templates 的文件夹,并在项目目录中创建 home.html 和 about.html。让我们也从flask导入render_template函数。
26.5创建模板在模板文件夹中创建 HTML 文件。
主页.html
< html lang =" en " >
< head >
< meta charset =" UTF-8 " />
<元 名称="viewport" content =" width=device-width, initial-scale=1.0 " / >
< title >首页 title >
head >
< body >
< h1 >Welcome Home h1 >
body >
html >
关于.html
< html lang =" en " >
< head >
< meta charset =" UTF-8 " />
<元 名称="viewport" content =" width=device-width, initial-scale=1.0 " / >
< title >关于 title >
head >
< body >
< h1 >关于我们 h1 >
body >
html >
26.6Python脚本
app.py
# 让我们
从 flask 导入 flask import Flask , render_template
import os # 导入操作系统模块
app = Flask ( __name__ )
@app.route('/') # 这个装饰器创建 home 路由
def home ():
return render_template ( 'home.html' )
@app.route('/about')
def about():
返回 render_template('about.html')
如果 __name__ == '__main__' :
#部署,我们使用ENVIRON
#使之成为生产和开发工作
port = int(os.environ.get("PORT", 5000))
app.run(debug=True, host='0.0.0.0', port=port)
如您所见,要转到不同的页面或导航,我们需要导航。让我们为每个页面添加一个链接,或者让我们创建一个用于每个页面的布局。
26.7导航现在,我们可以使用上面的链接在页面之间导航。让我们创建处理表单数据的附加页面。你可以叫它任何名字,我喜欢叫它 post.html。
我们可以使用 Jinja2 模板引擎将数据注入到 HTML 文件中。
# 让我们
从 flask 导入 flask import Flask , render_template , request , redirect , url_for
import os # 导入操作系统模块
app = Flask ( __name__ )
@app.route ( '/' ) # 这个装饰器创建 home 路由
def home ():
techs = [ 'HTML' , 'CSS' , 'Flask' , 'Python' ]
name = '30 Days Of Python Programming'
return render_template ( ' home.html' , techs = techs , name = name , title = 'Home' )
@app.route( '/about' )
def about ():
name = '30 Days Of Python Programming'
return render_template ( 'about.html' , name = name , title = 'about Us' )
@app.route( '/post' )
def post ():
name = 'Text Analyzer'
return render_template ( 'post.html' , name = name , title = name )
如果 __name__ == '__main__' :
#部署
#使之成为生产和开发工作
port = int(os.environ.get("PORT", 5000))
app.run(debug=True, host='0.0.0.0', port=port)
让我们也看看模板:
主页.html
< html lang =" en " >
< head >
< meta charset =" UTF-8 " />
<元 名称="视口" content =" width=device-width, initial-scale=1.0 " / >
< title >首页 title >
head >
Welcome to {{name}}
-
{% for tech in techs %}
- {{tech}} {% endfor %}


