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
from flask import Flask app = Flask(__name__) @app. route("/") def home(): return "Hello World!"
flask route params Parameters can be used when creating routes. A parameter can be a string (text) like this: /product/cookie . So you can pass parameters to your Flask route, can you pass numbers? The example here creates the route /sale/<transaction_id> , where transaction_id is a number.
A view function is the code you write to respond to requests to your application. Flask uses patterns to match the incoming request URL to the view that should handle it. The view returns data that Flask turns into an outgoing response.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With