在烧瓶中如何在另一个视图中调用来自另一个功能/路线的数据,如下所述

时间:2016-05-09 08:02:06

标签: python flask

以下3个链接并未解释我真正希望以外行的方式实现的目标

How do I call one Flask view from another one?

Get json from one view by calling it from another view

Call a route from within another route in Flask

我有以下代码

@app.route('/rate_isp_service', methods=['GET', 'POST'])
@login_required
def rate_isp_service():
isp_query = db.session.query(Isps)
isp_entries = [dict
               (isp_id=isp.isp_id, isp_name=isp.isp_name, isp_description=isp.isp_description) for isp in
               isp_query]

services_query = db.session.query(Services)
services_entries = [dict
                    (service_id=service.service_id, service_name=service.service_name,
                     service_catergory_id=service.service_catergory_id) for service in
                    services_query]

ratings_query = db.session.query(Ratings)
ratings_entries = [dict
                   (ratings_id=rating.ratings_id, rating_value=rating.rating_value,
                    rating_comment=rating.rating_comment) for rating in
                   ratings_query]

servicemetric_query = db.session.query(Service_metric)
servicemetric_entries = [dict
                         (metric_id=metric.metric_id, metric_name=metric.metric_name,
                          metric_description=metric.metric_description) for metric in
                         servicemetric_query]


return render_template('rate_isp_service.html', isp_entries=isp_entries, services_entries=services_entries,ratings_entries=ratings_entries)

这个代码填写我的html模板中的所有下拉列表,只要有表单。

我不得不在所有视图中多次包含此代码,因为我无法找到将其包含在某些视图中的方法

我想采取的方法是创建一个像这样的视图

@app.route('/dropdowns', methods=['GET', 'POST'])
@login_required
def dropdowns():
    that code here

并且能够在我想要的任何路线或视图中调用该dropdwon函数

1 个答案:

答案 0 :(得分:3)

为什么不把它放在一个函数中并随时调用它。像这样:

def new_func()
    isp_query = db.session.query(Isps)
    isp_entries = [dict
               (isp_id=isp.isp_id, isp_name=isp.isp_name,isp_description=isp.isp_description) for isp in
               isp_query]

    services_query = db.session.query(Services)
    services_entries = [dict
                    (service_id=service.service_id, service_name=service.service_name,
                     service_catergory_id=service.service_catergory_id) for service in
                    services_query]

    ratings_query = db.session.query(Ratings)
    ratings_entries = [dict
                   (ratings_id=rating.ratings_id,rating_value=rating.rating_value,
                    rating_comment=rating.rating_comment) for rating in
                   ratings_query]

    servicemetric_query = db.session.query(Service_metric)
    servicemetric_entries = [dict
                         (metric_id=metric.metric_id, metric_name=metric.metric_name,
                          metric_description=metric.metric_description) for metric in
                         servicemetric_query]

    return result1, result2, result3


@app.route('/rate_isp_service', methods=['GET', 'POST'])
@login_required
def rate_isp_service():
    result1, result2, result3 = new_func()

更好的方法是在同一个文件夹中创建 utils.py 并将此新函数放入其中,导入并在需要时使用它。