I'm working on a simple web page, where the user types an input sentence, and it outputs the permutations of the words in the input sentence (the calculation happens inside the permute
function, which I'm not posting here since not relevant).
After displaying the result (i.e. list of possible permutations), it doesn't show the link to the homepage. How could the link be shown?
This is my flask app:
from flask import Flask, request, render_template, url_for
from processing import permute
app = Flask(__name__)
app.config["DEBUG"] = True
@app.route("/", methods=["GET", "POST"])
def homepage():
errors = ""
if request.method == "POST":
input_sentence = request.form["input_sentence"]
if input_sentence:
result = permute(input_sentence)
return render_template("index.html", len = len(result), result = result)
return '''<html>
<body>
<p>Enter your sentence:</p>
<form method="post" action=".">
<p><input name="input_sentence" /></p>
<p><input type="submit" value="Permute your input!" /></p>
</form>
</body>
</html>
'''.format(errors=errors)
And this is the template:
<!DOCTYPE html>
<html>
<body>
<ol>
{%for i in range(0, len)%}
<li>{{result[i]}}</li>
{%endfor%}
</ol>
<a href="{{ url_for('homepage') }}">Click here to calculate again</a>
</body>
</html>
Earlier I wasn't using the render_template
. Instead I had:
result = permute(input_sentence)
return '''<html>
<body>
<p>The result is {result}</p>
<p><a href="/">Click here to calculate again</a>
</body>
</html>
'''.format(result=result)
And it was correctly redirecting to the homepage. It was displaying the output in a way I didn't like though, so I chose to use render_template
, which displayed the result as I wanted, but failed to show a link to the home page.
It seems like it's not reading the href line. Anybody can help?