利用裝飾器將url和函式關聯起來
# -*- coding:utf-8 -*-
class notflask():
def __init__(self):
self.routes = {}
def route(self,route_str): #用裝飾器函式的副產品去儲存乙個提供給我們的路徑
#之間的鏈結,裝飾器函式應該與它關聯起來。
#notflask物件加乙個「routes」字典,當我們的「decorator」函式被呼叫,
#路徑將被插入新字典中函式對應的位置。
def decorator(f):
self.routes[route_str] = f
return f
return decorator
def serve(self,path): #增加serve(path)函式:當路徑存在時返回結果,不然就報錯
view_function = self.routes.get(path)
if view_function:
return view_function()
else:
raise valueerror('route"{}""has not been registered')
def hello():
return 'hello,world'
執行結果: hello,world
mvc:model-view-controller,中文名「模型-檢視-控制器」
我理解為將html視為物件,更改物件內容和python內函式分開,非常方便
from flask import flask, request, render_template
def home():
return render_template('home.html')
def signin_form():
return render_template('form.html')
def signin():
username = request.form['username']
password = request.form['password']
if username=='admin' and password=='password':
return render_template('signin-ok.html', username=username)
return render_template('form.html', message='bad username or password', username=username)
if __name__ == '__main__':
//斜線
}
welcome,}!
python WEB框架Flask學習
from flask import flask def index return index if name main 方式一 方式二列如 settings.py class config debug true 連線mysql示例 database uri mysql pymysql 資料庫名 開發...
Python web框架Django學習(一)
注 我的是有兩個python版本,將python3.6版本的命令設定為 python3 這個根據自己的來就行 django admin startproject projectname 生成名為 projectname 的專案 之後需 cd projectname 執行後面命令 python3 ma...
python web開發框架
django python web應用開發框架 django 應該是最出名的python框架,gae甚至erlang都有框架受它影響。django是走大而全的方向,它最出名的是其全自動化的管理後台 只需要使用起orm,做簡單的物件定義,它就能自動生成資料庫結構 以及全功能的管理後台。diesel 基...