- source codes
- 样例演示, 有一天会失效
1 route 路由
语法
# 普通路由 @app.route('/') def index(): return 'Hello World!!!' # 路由传参 @app.route('/users/<name>') def user(name): return 'user: ' + name # 指定参数类型, 参考下面2 @app.route('/articles/<string:article_name>') def article(article_name): return 'article: ' + article_name # 指定 HTTP Methods 参考下面3 @app.route('/login', methods=['GET', 'POST']) def login(): if request.method == 'POST': do_the_login() else: show_the_login_form() # 获取表单数据 @app.route('/search', methods=['POST']) def search(): return 'key word: ' + request.form.get('search_key') + '<br> Find nothing.'The following converters exist:
string accepts any text without a slash (the default) int accepts integers float like intbut for floating point valuespath like the default but also accepts slashes any matches one of the items provided uuid accepts UUID strings Http Methods
GETThe browser tells the server to just get the information stored on that page and send it. This is probably the most common method.
HEADThe browser tells the server to get the information, but it is only interested in the headers, not the content of the page. An application is supposed to handle that as if a
GETrequest was received but to not deliver the actual content. In Flask you don’t have to deal with that at all, the underlying Werkzeug library handles that for you.POSTThe browser tells the server that it wants to post some new information to that URL and that the server must ensure the data is stored and only stored once. This is how HTML forms usually transmit data to the server.
PUTSimilar to
POSTbut the server might trigger the store procedure multiple times by overwriting the old values more than once. Now you might be asking why this is useful, but there are some good reasons to do it this way. Consider that the connection is lost during transmission: in this situation a system between the browser and the server might receive the request safely a second time without breaking things. WithPOSTthat would not be possible because it must only be triggered once.DELETERemove the information at the given location.
OPTIONSProvides a quick way for a client to figure out which methods are supported by this URL. Starting with Flask 0.6, this is implemented for
Flask web开发:整合jinja2、bootstrap、sqlite3与sqlalchemy实战

本文总结了使用Flask框架结合jinja2模板引擎、bootstrap前端框架、sqlite3数据库以及sqlalchemy ORM进行web开发的基础知识。涵盖了route路由、静态文件处理、模板继承、sqlite3数据库操作和sqlalchemy的使用。通过实例代码展示了如何创建路由、设置静态文件目录、实现模板继承以及数据库交互。
最低0.47元/天 解锁文章

557

被折叠的 条评论
为什么被折叠?



