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
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
Post a Comment