分路由类:
from flask import Flask, render_template, Blueprint
app = Flask(__name__)
test1 = Blueprint('test1', __name__)
@test1.route('/test')
def test():
return render_template('test1.html')
分路由类:
#encoding:utf-8
#!/usr/bin/env python
from flask import Flask, render_template, request, redirect, url_for, session, flash, Blueprint
import sys
reload(sys)
sys.setdefaultencoding('utf8')
app = Flask(__name__)
form_test = Blueprint('form_test', __name__)
app.config['SECRET_KEY'] = 'my'
app.secret_key = 'my'
@form_test.route('/index')
def index():
return render_template('test1.html')
@form_test.route('/login', methods=['get', 'post'])
def login():
name = request.form.get('name')
password = request.form.get('password')
if name == 'admin' and password == '123':
session['name'] = name
return redirect(url_for('login'))
if name != 'admin':
flash('no this name')
return render_template('test1.html', name=session.get('name'))
主路由启动类:
#encoding:utf-8
#!/usr/bin/env python
from flask import Flask, render_template
from test1 import test1
from form_test import form_test
from flask_script import Manager
app = Flask(__name__)
app.register_blueprint(form_test)
app.register_blueprint(test1)
@app.route('/')
def index():
num = ['tom', 'mike', 'amuxia', 'zhao', 'lisi']
return render_template('/index.html', num=num)
@app.errorhandler(404)
def miss(e):
return render_template('404.html'), 404
@app.errorhandler(500)
def error(e):
return render_template('500.html'), 500
if __name__=='__main__':
print(app.url_map)
app.run()
看以上代码大家应该也明白了,分路由中由form_test = Blueprint('form_test', __name__)创建蓝图,而后在主路由中通过app.register_blueprint(form_test)注入。