简体   繁体   中英

In flask how do i call data from another function/route in another view as explained below

The following 3 links are not explaining what i really want to achieve in layman's terms

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

I have the following code

@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)

And this code populates all my drop downs in my html templates wherever there is a form.

I have had to include this code multiple times in all the views since i cant find a way to include it in some of the views

The approach i wanted to take was creating a view like this

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

and be able to call that dropdwon function in any route or view i want

Why don't you put it in a function and call it whenever you want. Like this:

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()

A better approach would be to make a utils.py in the same folder and put this new function in it, import it and use it whenever needed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM