国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python編程在flask中模擬進行Restful的CRUD操作

2020-02-16 00:22:40
字體:
來源:轉載
供稿:網友

這篇文章中我們將通過對HelloWorld的message進行操作,介紹一下如何使用flask進行Restful的CRUD。

概要信息

事前準備:flask

liumiaocn:flask liumiao$ which flask/usr/local/bin/flaskliumiaocn:flask liumiao$ flask --versionFlask 1.0.2Python 2.7.10 (default, Jul 15 2017, 17:16:57) [GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.31)]liumiaocn:flask liumiao$

代碼示例:HTTP謂詞(GET)

就像angular的插值表達式在模版中的作用一樣,在flask中也可以一樣使用,如果不熟悉angular的插值表達式的話也不要緊,看完下面的例子,基本上就會有一個大致的印象。

代碼示例

liumiaocn:flask liumiao$ cat flask_4.py #!/usr/bin/pythonfrom flask import Flaskfrom flask import render_templateapp = Flask(__name__)greeting_messages=["Hello World", "Hello Python"]@app.route("/api/messages",methods=['GET'])def get_messages():  return render_template("resttest.html",messages=greeting_messages) if __name__ == "__main__":  app.debug=True  app.run(host='0.0.0.0',port=7000)liumiaocn:flask liumiao$

模版文件

liumiaocn:flask liumiao$ cat templates/resttest.html <!DOCTYPE html><html><head> <meta charset="utf-8"> <title>Hello Restful</title></head><body>    {% for message in messages %} <h1>{{ message }}</h1>    {% endfor %}</body></html>liumiaocn:flask liumiao$

代碼解析:app.route中指定了HTTP謂詞GET,缺省GET可以省略,如果一個方法對應多個謂詞動作,通過request.method來分離時,可以寫成methods=[‘GET','POST']的形式

執行&確認

liumiaocn:flask liumiao$ ./flask_4.py  * Serving Flask app "flask_4" (lazy loading) * Environment: production  WARNING: Do not use the development server in a production environment.  Use a production WSGI server instead. * Debug mode: on * Running on http://0.0.0.0:7000/ (Press CTRL+C to quit) * Restarting with stat * Debugger is active! * Debugger PIN: 131-533-062

頁面確認

代碼示例:HTTP謂詞(DELETE|PUT|POST)

liumiaocn:flask liumiao$ cat flask_4.py #!/usr/bin/pythonfrom flask import Flaskfrom flask import render_templatefrom flask import requestimport jsonapp = Flask(__name__)greeting_messages=["Hello World", "Hello Python"]#HTTP: GET: Retrieve operation@app.route("/api/messages",methods=['GET'])def get_messages():  return render_template("resttest.html",messages=greeting_messages) #HTTP: DELETE: Delete operation@app.route("/api/messages/<messageid>",methods=['DELETE'])def delete_message(messageid):  global greeting_messages  del greeting_messages[int(messageid)]  return render_template("resttest.html",messages=greeting_messages) #HTTP: PUT: Update operation#HTTP: POST: Create operation@app.route("/api/messages/<messageid>",methods=['PUT','POST'])def update_message(messageid):  global greeting_message  msg_info=json.loads(request.get_data(True,True,False))  #msg_info=request.args.get('message_info')  #msg_info=request.form.get('message_info','default value')  #msg_info=request.values.get('message_info','hello...')  greeting_messages.append("Hello " + msg_info["message_info"])  return render_template("resttest.html",messages=greeting_messages) if __name__ == "__main__":  app.debug=True  app.run(host='0.0.0.0',port=7000)liumiaocn:flask liumiao$            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 西乡县| 大化| 大同县| 大兴区| 潮安县| 界首市| 安泽县| 哈尔滨市| 汝城县| 师宗县| 麻栗坡县| 康马县| 济南市| 莱西市| 来宾市| 千阳县| 绍兴县| 玉溪市| 香港| 洱源县| 阳东县| 微山县| 锡林郭勒盟| 霍城县| 锦州市| 葫芦岛市| 黄骅市| 普陀区| 元阳县| 江安县| 莒南县| 潢川县| 榆社县| 岗巴县| 确山县| 凯里市| 罗甸县| 四川省| 阿合奇县| 漠河县| 雷州市|