<p>这是我的解决方法:</p>
<p>导入蓝图时,我定义嵌套的路由:</p>
<pre><code>app.register_blueprint(product_endpoints, url_prefix='/sites/<int:site_id>/menus/<int:menu_id>/categories/<int:category_id>/products/<int:product_id>')
app.register_blueprint(category_endpoints, url_prefix='/sites/<int:site_id>/menus/<int:menu_id>/categories/<int:category_id>')
app.register_blueprint(menu_endpoints, url_prefix='/sites/<int:site_id>/menus/<int:menu_id>')
app.register_blueprint(site_endpoints, url_prefix='/sites/<int:site_id>')
</code></pre>
<p>在设计图中,我重用了路由解析函数。例如,在<code>product_endpoints</code>文件中:</p>
<pre><code>from category_endpoints import get_category_data
product_endpoints = Blueprint('product_endpoints', __name__)
@product_endpoints.url_value_preprocessor
def get_product_data(endpoint, values):
if 'category_id' in values:
get_category_data(endpoint, values)
product = Product.get_by_id(int(values.pop('product_id')))
if not product:
abort(404)
g.product = product
</code></pre>
<p>在<code>category_endpoints</code>文件中:</p>
<pre><code>from menu_endpoints import get_menu_data
category_endpoints = Blueprint('category_endpoints', __name__)
@category_endpoints.url_value_preprocessor
def get_category_data(endpoint, values):
if 'menu_id' in values:
get_menu_data(endpoint, values)
category = ProductCategory.get_by_id(int(values.pop('category_id')))
if not category:
abort(404)
g.category = category
</code></pre>
<p>等等。。。使用这种方法,我的蓝图也可以用于直接路由,如<code>/products/<int:product_id></code>。</p>
<p>这种方法对我很有效。我希望它也能帮助你。</p>