1. 程式人生 > >python3 web框架(三、MVC與MTV)

python3 web框架(三、MVC與MTV)

我們已經寫完了一個簡單的web框架,那麼這裡

def handle_index():
    return ["<h1>hello web</h1>".encode('utf-8')]
 

返回的內容我們是自已一字串的形式寫出來的,那麼還可以去一個檔案裡讀取寫好的內容,我們可以這麼操作。

同級目錄下新建一個名為 index.html 的檔案 

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>hello web</h1>
</body>
</html>

然後我們訪問的之後直接讀取這個檔案就可以了

app.py

from wsgiref.simple_server import make_server 
 
def handle_index():
    f=open('index.html',mode='rb')
    data = f.read()
    f.close()
    return [data,]
 
#def handle_hello():
#    return ["<h1>here is hello</h1>".encode('utf-8')]
 
#def handle_data():
#    return ["<h1>here is data</h1>".encode('utf-8')]
 
#對應關係
URL_DICT = {
        '/':handle_index,
            }
#URL_DICT = {
#        '/':handle_index,
#        '/hello':handle_hello,
#        '/data':handle_data,
#            }



def handle_request(env, res):
    res("200 OK",[("Content-Type","text/html")]) 
    acqu_url = env['PATH_INFO']#
    #body = "<h1>127.0.0.1:8000%s</h1>"%str(acqu_url)
 
    func = None
    if acqu_url in URL_DICT:
        func = URL_DICT[acqu_url]
    if func:
        return func()
    else:
        return ["<h1>404</h1>".encode('utf-8')]
 
#    return [body.encode('utf-8')] 
    
if __name__ == "__main__": 
    httpd = make_server("127.0.0.1",8000,handle_request) 
    print("Serving http on port 8000") 
    httpd.serve_forever() 

但是這樣做的話,我們現在的目錄結構是這個樣子的

這裡我們才一個url如果很多url 很多html檔案,那麼目錄就很混亂,此時我們選擇新建一個資料夾 View 來存放html檔案。我們app.py中這裡也要做相應的改動:

def handle_index():
    f=open('View/index.html',mode='rb')
    data = f.read()
    f.close()
    return [data,]

這樣做完了以後我們又發現一個問題,我們app.py中 像 handle_index() 這樣的函式很多的話,我們的程式碼還是很亂,那麼我們還應該把這樣的函式獨立整理出來,我們新建一個資料夾Controller,在Controller下新建一個urls_res.py檔案來寫這樣的函式;

urls_res.py

def handle_index():
    f=open('View/index.html',mode='rb')
    data = f.read()
    f.close()
    return [data,]

app.py

from wsgiref.simple_server import make_server 
from  Controller import urls_res

#對應關係
URL_DICT = {
        '/':urls_res.handle_index,
            }
#這裡路由改一下

def handle_request(env, res):
    res("200 OK",[("Content-Type","text/html")]) 
    acqu_url = env['PATH_INFO']#
    #body = "<h1>127.0.0.1:8000%s</h1>"%str(acqu_url)
 
    func = None
    if acqu_url in URL_DICT:
        func = URL_DICT[acqu_url]
    if func:
        return func()
    else:
        return ["<h1>404</h1>".encode('utf-8')]
 
#    return [body.encode('utf-8')] 
    
if __name__ == "__main__": 
    httpd = make_server("127.0.0.1",8000,handle_request) 
    print("Serving http on port 8000") 
    httpd.serve_forever() 

目錄結構

這個時候如果需要寫業務程式碼,就在Controller裡新建檔案,如果需要新增模板,往View裡面新增就可以了。

寫到這裡我們發現好像忘記了什麼?——————資料庫

我們在html中寫上一個特殊的語句跟資料庫中的資料進行替換,比如在 hello web 後面寫上@abc:

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>hello web @abc</h1>
</body>
</html>

我們在讀完 index.html檔案後 做個替換。

urls_res.py

def handle_index():
    f=open('View/index.html',mode='rb')
    data = f.read()
    data = data.replace('@abc'.encode('utf-8'),'here is abc'.encode('utf-8'))#假設這裡'here is abc'是資料庫中的資料,我們把@abc替換為這條資料
    
    f.close()
    return [data,]

這裡我們沒有進行資料庫操作直接使用的一個假設的資料來進行替代,實際上如果我們想使用資料庫中的資料還要在handle_index()函式中進行資料庫的操作,但是這樣一來,我們的urls_res.py中的程式碼又會變得很混亂,為了解決這個問題,我們再建一個資料夾Modle, 在Modle新建檔案來寫程式碼進行資料庫的操作。

解決的上述問題,我們新建了三個資料夾  Modle 、 View 和 Controller。

這三個資料夾都有自己相對應的功能Modle--資料庫操作、 View--模板檔案 和 Controller--業務處理,而他們的首字母合起來就叫 MVC ,這就是MVC,對檔案的分類,對功能的劃分。

而MTV跟MVC 本質上是一樣的,只不過資料夾的名字不一樣,MTV中的M也是Modle,進行資料庫操作;T指Template ,裡面存放的是模板檔案;V也叫View,但是這裡的View是業務處理。