Issue
I thought this would be simple, but maybe not.
I have a form in HTML called home.html
which contains the following form:
<form method="POST">
<textarea name="query_text" id="query_text" class="form-control"></textarea>
<button type="submit" name="submit" class="btn btn-primary">Submit</button>
</form>
I then have a python file called views.py
with the following text:
@views.route('/', methods=['GET', 'POST'])
def home():
if request.method == 'POST':
query_text = request.form.get('query_text')
db.session.add(query_text)
db.session.commit()
return render_template("home.html")
This is all working great. However as the render_template
reloads the page, it removes the text in the textarea
. This is an unwanted behaviour as I want the user to tweak the query, rather than remember and write it out again.
Is there a way of leaving the text in place when submitting?
Solution
Figured it out finally. Going to leave here for anyone in the future:
Added a new variable in the render_template called foo_bar
which has the text from the textarea
:
return render_template("home.html", foo_bar = query_text)
I then added that variable 'foo_bar' within 2 curly brackets to be treated as a variable if it exists as the textarea text:
<form method="POST">
<textarea name="query_text" id="query_text" class="form-control">{{ foo_bar }}</textarea>
<button type="submit" name="submit" class="btn btn-primary">Submit</button>
</form>
This works perfectly
Answered By - Nicholas
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.