我想实现简单的站点布局:
/
必须呈现home.html
/one
,/two
,/three
必须相应地呈现one.html
,two.html
,three.html
到目前为止,我想出了以下代码:
main_page = Blueprint('main', __name__)
category_page = Blueprint('category', __name__)
@main_page.route("/")
def home():
return render_template('home.html')
@category_page.route('/<category>')
def show(category):
return render_template('{}.html'.format(category))
app = Flask(__name__)
app.register_blueprint(main_page, url_prefix='/')
app.register_blueprint(category_page, url_prefix='/categories')
这样我就可以将categories
路由到/categories/<category>
。如何将它们路由到/<category>
,同时保持home.html
链接到{
我尝试了两种方法:
为两个蓝图设置url_prefix='/'
=>;第二个蓝图不起作用。
不要使用main_page
蓝图,只需使用app.route('/')
来呈现{category_page
blueprint混合时,这个也不起作用
可以将变量移动到registing语句内的
url_prefix
参数:(这取决于整个模式的复杂性,但最好将带有变量的注册语句保持在每个函数附近,以处理多个视图。)
相关问题 更多 >
编程相关推荐