您好,登錄后才能下訂單哦!
這篇文章主要介紹如何使用flask進行Restful的CRUD,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
概要信息
事前準備:flask
liumiaocn:flask liumiao$ which flask /usr/local/bin/flask liumiaocn:flask liumiao$ flask --version Flask 1.0.2 Python 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/python from flask import Flask from flask import render_template app = 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 %} <h2>{{ message }}</h2> {% endfor %} </body> </html> liumiaocn:flask liumiao$
代碼解析:app.route中指定了HTTP謂詞GET,缺省GET可以省略,如果一個方法對應(yīng)多個謂詞動作,通過request.method來分離時,可以寫成methods=[‘GET','POST']的形式
執(zhí)行&確認
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/python from flask import Flask from flask import render_template from flask import request import json app = 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$
執(zhí)行&結(jié)果確認
執(zhí)行日志
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
結(jié)果確認:Delete
liumiaocn:flask liumiao$ curl -X DELETE http://localhost:7000/api/messages/1 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Hello Restful</title> </head> <body> <h2>Hello World</h2> </body> </html>liumiaocn:flask liumiao$
可以看到執(zhí)行一次DELETE之后,兩條消息現(xiàn)在只剩下一條消息了,接下來使用POST添加再添加一條
liumiaocn:flask liumiao$ curl -X POST -d '{"message_info":"LiuMiaoPost"}' http://localhost:7000/api/messages/3 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Hello Restful</title> </head> <body> <h2>Hello World</h2> <h2>Hello LiuMiaoPost</h2> </body> </html>liumiaocn:flask liumiao$
再執(zhí)行一次PUT操作
liumiaocn:flask liumiao$ curl -X PUT -d '{"message_info":"LiuMiaoPut"}' http://localhost:7000/api/messages/4 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Hello Restful</title> </head> <body> <h2>Hello World</h2> <h2>Hello LiuMiaoPost</h2> <h2>Hello LiuMiaoPut</h2> </body> </html>liumiaocn:flask liumiao$
以上是“如何使用flask進行Restful的CRUD”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。