当前位置:网站首页>Mini web framework: template replacement and routing list function development | dark horse programmer

Mini web framework: template replacement and routing list function development | dark horse programmer

2022-06-22 19:35:00 Dark horse programmer official

One 、 Template replacement function development

1. Read the stock information template file

framework.py Sample code :

#  Get home page data 
def index():
    #  Response state 
    status = "200 OK";
    #  Response head 
    response_header = [("Server", "PWS2.0")]

    #  Open template file , Reading data 
    with open("template/index.html", "r") as file:
        file_data = file.read()

2. Replace template variables with analog data

framework.py Sample code :

#  Get home page data 
def index():
    #  Response state 
    status = "200 OK";
    #  Response head 
    response_header = [("Server", "PWS2.0")]

    # 1.  Open template file , Reading data 
    with open("template/index.html", "r") as file:
        file_data = file.read()

    #  Processed data ,  Query from database 
    data = time.ctime()
    # 2.  Replace the template in the template file 
    result = file_data.replace("{%content%}", data)

    return status, response_header, result

Two 、 Routing list function development

1. Introduction to routing

Then the judgment scenario of the above program , If we deal with the dynamic resource request of a personal center, it is very simple , Add one more function and one more branch judgment to realize .

framework.py Sample code :

#  Get personal center data 
def center():
    #  Response state 
    status = "200 OK";
    #  Response head 
    response_header = [("Server", "PWS2.0")]

    #  Open template file , Reading data 
    with open("template/center.html", "r") as file:
        file_data = file.read()

    #  Processed data ,  Query from database 
    data = time.ctime()
    #  Replace the template in the template file 
    result = file_data.replace("{%content%}", data)

    return status, response_header, result


#  Handle dynamic resource requests 
def handle_request(env):
    #  Get the dynamic request resource path 
    request_path = env["request_path"]
    print(" Dynamic resource request received :", request_path)

    if request_path == "/index.html":
        #  Get home page data 
        result = index()
        return result
    elif request_path == "/center.html":
        #  Get personal center data 
        result = center()
        return result
    else:
        #  Dynamic resource not found 
        result = not_found()
        return result

If our framework handles more page request paths , such as :5 Path judgment , You may feel that the conditional branch is completely competent , If it is 40 Even more ? If this is still a normal conditional branch, it is simply unbearable .

terms of settlement :  You can use routing

What is routing ?

Routing is the request URL Mapping to handler , That is to say, send the request in advance URL It is well associated with the handler .

The routing list

How to manage so many routes , You can use a routing list to manage , Save each route through the route list .

Request path Processing function
/login.htmllogin function
/index.htmlindex function
/center.htmlcenter function

2. Add a route to the route list

framework.py Sample code :

#  Define routing list 
route_list = [
    ("/index.html", index),
    ("/center.html", center)
]

3. Traverse the routing list according to the user request to process the user request

framework.py Sample code :


#  Handle dynamic resource requests 
def handle_request(env):
    #  Get the dynamic request resource path 
    request_path = env["request_path"]
    print(" Dynamic resource request received :", request_path)
    #  Traverse the routing list , Select the function to execute 
    for path, func in route_list:
        if request_path == path:
            result = func()
            return result
    else:
        #  Dynamic resource not found 
        result = not_found()
        return result

    # if request_path == "/index.html":
    #     #  Get home page data 
    #     result = index()
    #     return result
    # elif request_path == "/center.html":
    #     #  Get personal center data 
    #     result = center()
    #     return result
    # else:
    #     #  Dynamic resource not found 
    #     result = not_found()
    #     return result

原网站

版权声明
本文为[Dark horse programmer official]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206221755475724.html