荒野的flask学习笔记

󰃭 2017-05-09

荒野的flask学习笔记(1)

导入类 实例化 name传递包或模块的名称

from flask import Flask, url_for, render_template
app = Flask(__name__)

装饰器rout(’/’)确定触发函数的URL 或者说函数绑定到URL上

@app.route('/')
def index():
	return 'Index Page'

采用render_template渲染模板

@app.route('/hello/')
@app.route('/hello/<name>')
def hello(name=None):
	return render_template('hello.html', name=name)

给url增加变量

@app.route('/user/<username>')
def show_user_profile(username):
    return 'User %s' %username

@app.route('/post/<int:post_id>')
def show_post(post_id):
	return 'Post %d' %post_id
@app.route('/projects')
def projects():
	return 'The project page'

@app.route('/about')
def about():
	return 'The about page'

==登录,待补充完善==

@app.route('/login', methods=['GET','POST'])
def login():
	if request.method == 'POST':
		do_the_login()
	else:
		show_the_login_form()

==静态文件==

url_for('static', filename= 'style.css')

RUN

if __name__ == '__main__':
	app.run(host='0.0.0.0')