5

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

Community
  • 1
  • 1
Chamambom
  • 127
  • 1
  • 1
  • 10

1 Answers1

5

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.

Amin Alaee
  • 1,895
  • 17
  • 26
  • ,the utils.py method seems to work perfectly and i like it because it makes my code very clean.Can you explain a bit more what this line actually mean `result1, result2, result3 = new_func()` because i thought just calling the function name `new_func()` was supposed to bring along the return values – Chamambom May 09 '16 at 09:47