Flask is not re-rendering the template page with the new variable.
Flask code (app.py):
from flask import Flask, render_template, request, redirect, url_for
app = Flask(__name__)
@app.route("/",methods = ['GET', 'POST'])
@app.route("/<value>",methods = ['GET', 'POST'])
def index(value=1):
print(value)
return render_template('home.html',number=value)
@app.route("/next",methods = ['GET', 'POST'])
def next():
if request.method == 'POST':
last_num = list(dict(request.form).keys())[0]
last_num = int(last_num)
return redirect(url_for("index",value=last_num+1))
if __name__ == '__main__':
app.run()
HTML page (home.html):
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(function() {
$('#submit').click(function() {
$.ajax({
url: '/next',
data: String(Number(document.getElementById("number").textContent)),
type: 'POST',
success: function(response) {
console.log("Awesome");
},
error: function(error) {
console.log(error);
}
});
});
});
</script>
</head>
<body>
<div id="number">
{{number}}
</div>
<button type="button" id="submit">Next</button>
</body>
</html>
The (last) number in HTML page nicely reaches the flask code and the index
function gets the incremented value also. But the HTML page do not reload with the new number
variable.