当前位置:网站首页>mini-Web框架:模板替换与路由列表功能开发 | 黑马程序员
mini-Web框架:模板替换与路由列表功能开发 | 黑马程序员
2022-06-22 17:56:00 【黑马程序员官方】
一、模板替换功能开发
1. 读取股票信息模板文件
framework.py示例代码:
# 获取首页数据
def index():
# 响应状态
status = "200 OK";
# 响应头
response_header = [("Server", "PWS2.0")]
# 打开模板文件,读取数据
with open("template/index.html", "r") as file:
file_data = file.read()
2. 使用模拟数据替换模板变量
framework.py示例代码:
# 获取首页数据
def index():
# 响应状态
status = "200 OK";
# 响应头
response_header = [("Server", "PWS2.0")]
# 1. 打开模板文件,读取数据
with open("template/index.html", "r") as file:
file_data = file.read()
# 处理后的数据, 从数据库查询
data = time.ctime()
# 2. 替换模板文件中的模板遍历
result = file_data.replace("{%content%}", data)
return status, response_header, result
二、路由列表功能开发
1. 路由的介绍
接着上面程序的判断场景,假如咱们再处理一个个人中心的动态资源请求非常简单,再添加一个函数和更加一个分支判断就可以实现了。
framework.py 示例代码:
# 获取个人中心数据
def center():
# 响应状态
status = "200 OK";
# 响应头
response_header = [("Server", "PWS2.0")]
# 打开模板文件,读取数据
with open("template/center.html", "r") as file:
file_data = file.read()
# 处理后的数据, 从数据库查询
data = time.ctime()
# 替换模板文件中的模板遍历
result = file_data.replace("{%content%}", data)
return status, response_header, result
# 处理动态资源请求
def handle_request(env):
# 获取动态请求资源路径
request_path = env["request_path"]
print("接收到的动态资源请求:", request_path)
if request_path == "/index.html":
# 获取首页数据
result = index()
return result
elif request_path == "/center.html":
# 获取个人中心数据
result = center()
return result
else:
# 没有找到动态资源
result = not_found()
return result
那如果咱们的框架处理的页面请求路径再多一些,比如:5个路径判断,大家可能感觉条件分支完全可以胜任,如果是40个甚至更多呢? 如果这是还是用普通的条件分支简直无法忍受。
解决办法: 可以使用路由
什么是路由?
路由就是请求的URL到处理函数的映射,也就是说提前把请求的URL和处理函数关联好。
路由列表
这么多的路由如何管理呢, 可以使用一个路由列表进行管理,通过路由列表保存每一个路由。
| 请求路径 | 处理函数 |
|---|---|
| /login.html | login函数 |
| /index.html | index函数 |
| /center.html | center函数 |
2. 在路由列表添加路由
framework.py 示例代码:
# 定义路由列表
route_list = [
("/index.html", index),
("/center.html", center)
]
3. 根据用户请求遍历路由列表处理用户请求
framework.py 示例代码:
# 处理动态资源请求
def handle_request(env):
# 获取动态请求资源路径
request_path = env["request_path"]
print("接收到的动态资源请求:", request_path)
# 遍历路由列表,选择执行的函数
for path, func in route_list:
if request_path == path:
result = func()
return result
else:
# 没有找到动态资源
result = not_found()
return result
# if request_path == "/index.html":
# # 获取首页数据
# result = index()
# return result
# elif request_path == "/center.html":
# # 获取个人中心数据
# result = center()
# return result
# else:
# # 没有找到动态资源
# result = not_found()
# return result
边栏推荐
- Sre is bound to move towards the era of chaotic engineering -- Huawei cloud chaotic engineering practice
- Aiops intelligent operation and maintenance experience sharing
- Flutter2.0运行在web上不同渲染器的问题
- 下拉刷新及上拉加载更多的ListView
- Some technical ideas:
- 同花顺难开户么?网上开户安全么?
- 函数的导数与微分的关系
- Golang 實現 Redis(10): 本地原子性事務
- AIOps 智能运维经验分享
- 預訓練語言模型,bert,RoFormer-Sim又稱SimBERTv2
猜你喜欢

China's games are "harvesting" foreigners

IPLOOK和思博伦通信建立长期合作

UE4_UE5制作3DUI-跟随相机朝向(附工程)

Cookie encryption 3+rpc solution

Iplook, as a member of o-ran alliance, will jointly promote the development of 5g industry
Getting started with database connection pooling (c3p0, Druid)

Implementing Domain Driven Design - using ABP framework - solution overview

5gc and satellite integrated communication scheme

新人报道的笔记
组合学笔记(五)分配格中的链
随机推荐
IPLOOK作为O-RAN联盟会员,将共同促进5G产业发展
Implementing Domain Driven Design - using ABP framework - solution overview
预训练语言模型,bert,RoFormer-Sim又称SimBERTv2
SRE必将走向混沌工程时代--华为云混沌工程实践
Niuke.com: judge whether it is palindrome string
5g short message solution
Exness sorted out three problems to be solved in Musk's acquisition of Twitter
在循环中动态改变标签元素的样式
Modèle de langage de pré - formation, Bert, roformer Sim aussi connu sous le nom de simbertv2
預訓練語言模型,bert,RoFormer-Sim又稱SimBERTv2
Golang implements reliable delay queue based on redis
回文数(简单版)
泡泡玛特:空洞的灵魂需要故事
Sre is bound to move towards the era of chaotic engineering -- Huawei cloud chaotic engineering practice
Pre training language model, Bert, roformer SIM, also known as simbertv2
一些技术想法:
DBMS in Oracle_ output. put_ Example of line usage
2022 R2 mobile pressure vessel filling test question simulation test platform operation
贪心之区间问题(2)
session机制详解以及session的相关应用