Python 两个烧瓶蓝图的相同url_前缀

Python 两个烧瓶蓝图的相同url_前缀,python,flask,routing,Python,Flask,Routing,我想实现简单的站点布局: /必须呈现home.html /one,/two,/three必须相应地呈现one.html,two.html,three.html 到目前为止,我提出了以下代码: main\u page=Blueprint('main',\u名称\u) 类别页面=蓝图(“类别”,“名称”) @主页面路线(“/”) def home(): 返回渲染模板('home.html') @类别\第页路线(“/”) def显示(类别): 返回render_模板(“{}.html.”格式(类别))

我想实现简单的站点布局:

/
必须呈现
home.html

/one
/two
/three
必须相应地呈现
one.html
two.html
three.html

到目前为止,我提出了以下代码:

main\u page=Blueprint('main',\u名称\u)
类别页面=蓝图(“类别”,“名称”)
@主页面路线(“/”)
def home():
返回渲染模板('home.html')
@类别\第页路线(“/”)
def显示(类别):
返回render_模板(“{}.html.”格式(类别))
app=烧瓶(名称)
app.register_blueprint(主页,url_前缀='/'))
应用程序注册蓝图(类别页面,url前缀='/categories')
这样我就可以将
类别
路由到
/categories/
。如何将它们路由到
/
,同时保持
home.html
链接到
/
?谢谢你的帮助

我尝试了两种方法:

  • 为两个蓝图设置
    url\u prefix='/'
    ,第二个蓝图不起作用

  • 不要使用
    main\u页面
    blueprint,只需使用
    app.route('/')
    来呈现
    home.html
    。当与
    category\u页面混用时,此页面也不起作用


  • 您可以将变量移动到注册语句中的
    url\u prefix
    参数:

    @main_page.route("/")
    def home():
        return render_template('home.html')
    app.register_blueprint(main_page, url_prefix='/')
    
    @category_page.route('/')
    def show(category):
        return render_template('{}.html'.format(category))        
    app.register_blueprint(category_page, url_prefix='/<category>')
    
    @main\u page.route(“/”)
    def home():
    返回渲染模板('home.html')
    app.register_blueprint(主页,url_前缀='/'))
    @类别\第页路线(“/”)
    def显示(类别):
    返回render_模板(“{}.html.”格式(类别))
    应用程序注册蓝图(类别页面,url前缀='/'))
    

    (这取决于整个模式的复杂性,但最好将带有变量的注册语句放在每个函数附近,以处理许多视图。)

    非常感谢!这几乎太容易了:)我同意,这里所有的工作都是你做的!!我喜欢这种模式,即使它可能不方便处理许多路由和许多变量,除非您使注册部分远离函数