1.一個(gè)簡(jiǎn)單的應(yīng)用
#app.py
from flask import Flask
app = Flask('__name__')
@app.route('/')
def home():
return 'hello world'
if __name__ == '__main__':
app.run()
命令行執(zhí)行 python app.py
瀏覽器輸入:http://localhost:5000/

simpleapp.png
2.跳轉(zhuǎn)到靜態(tài)html
#app.py
from flask import Flask
app = Flask('__name__', static_url_path='')
@app.route('/')
def home():
return app.send_static_file('view/index.html')
@app.route('/sigin', methods=['GET'])
def singin():
return app.send_static_file('view/singin.html')
@app.route('/sigin', methods=['POST'])
def main():
return app.send_static_file('view/main.html')
if __name__ == '__main__':
app.run()
- static_url_path: 指定靜態(tài)文件路徑,默認(rèn)文件夾名static
- app.send_static_file:發(fā)送靜態(tài)文件。
文件結(jié)構(gòu):

圖片.png
- index.html
<h1>Home</h1>
- singin.html
<html>
<form action="/sigin" method="post">
<p><input name="username"></p>
<p><input name="password"></p>
<p><button type="submit">Sign In</button></p>
</form>
</html>
- main.html
<h3>hello world</h3>
3.模板
-
做一個(gè)模板
默認(rèn)情況下,模板會(huì)從templates路徑下加載。
圖片.png
模板內(nèi)容大致如下:
<html>
<head>
<title>Error Page</title>
</head>
<body>
<p>Username ( {{ username }} )is error or password ({{pwd}}) is error</p>
</body>
</html>
- 使用這個(gè)模板
使用flask里面的render_template方法返回這個(gè)模板。具體代碼是把
之前那個(gè)app.py內(nèi)容改成下面這樣:
from flask import Flask, request, render_template
app = Flask('__name__', static_url_path='')
@app.route('/')
def home():
return app.send_static_file('view/index.html')
@app.route('/sigin', methods=['GET'])
def singin():
return app.send_static_file('view/singin.html')
@app.route('/sigin', methods=['POST'])
def main():
name = request.form['username']
password = request.form['password']
if (name == 'user' and password == 'pwd'):
return app.send_static_file('view/main.html')
else:
return render_template('error.html', username=name, pwd=password)
if __name__ == '__main__':
app.run()
- render_template 函數(shù)
這個(gè)函數(shù)主要是邏輯是:
傳入數(shù)據(jù)和模板 組裝 ?組裝后數(shù)據(jù)send給前臺(tái)
def render_template(template_name_or_list, **context):
"""Renders a template from the template folder with the given
context.
:param template_name_or_list: the name of the template to be
rendered, or an iterable with template names
the first one existing will be rendered
:param context: the variables that should be available in the
context of the template.
"""
ctx = _app_ctx_stack.top
ctx.app.update_template_context(context)
return _render(ctx.app.jinja_env.get_or_select_template(template_name_or_list),
context, ctx.app)
def _render(template, context, app):
"""Renders the template and fires the signal"""
before_render_template.send(app, template=template, context=context)
rv = template.render(context)
template_rendered.send(app, template=template, context=context)
return rv
參考
flask官網(wǎng): http://flask.pocoo.org/docs/0.12/quickstart/#a-minimal-application
廖雪峰Python:https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/001432012745805707cb9f00a484d968c72dbb7cfc90b91000
