Instead of divs
, you can use buttons. That way, ajax
can be utilized in the front end to retrieve the id of the button clicked and pass it to the backend:
"index.html"
:
<html>
<body>
<button id='1'>Button1</button>
<button id='2'>Button2</button>
<button id='3'>Button3</button>
</body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
$('button').click(function(event) {
var the_id = event.target.id;
$.ajax({
url: "/get_id",
type: "get",
data: {the_id: the_id},
success: function(response) {
window.location.replace('/experts');
},
error: function(xhr) {
//Do Something to handle error
}
});
});
</script>
</html>
Then, a route to receive the id can be created, the results from the ajax
can be stored in flask.session
, and a "success" object can be passed back to the ajax
in the index.html
template. From the jquery in the template, the app can be redirected to /expert
:
import flask
app = flask.Flask(__name__)
app.secret_key = 'SOME_SECRET_KEY'
@app.route('/get_id')
def expert():
button_id = flask.request.args.get('the_id')
flask.session['button_id'] = button_id
return flask.jsonify({'success':True})
@app.route('/experts', methods=['GET'])
def experts():
return render_template("experts.html", data=flask.session['button_id'])