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


the following 3 links not explaining want achieve in layman's terms

how call 1 flask view one?

get json 1 view calling view

call route within route in flask

i have 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) 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) 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) 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) metric in                          servicemetric_query]   return render_template('rate_isp_service.html', isp_entries=isp_entries, services_entries=services_entries,ratings_entries=ratings_entries) 

and code populates drop downs in html templates wherever there form.

i have had include code multiple times in views since cant find way include in of views

the approach wanted take creating view

@app.route('/dropdowns', methods=['get', 'post']) @login_required def dropdowns():     code here 

and able call dropdwon function in route or view want

why don't put in function , call whenever want. 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) 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) 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) 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) 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 make utils.py in same folder , put new function in it, import , use whenever needed.


Comments

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -