Josh Friedlander
Josh Friedlander

Reputation: 11657

Changing an HTML file through Flask

I have an HTML form (in a very basic Flask site, adjusted to be inside a class), which takes in some values and will upload them to a database. What I'd like is to send the user some kind of a message on the same page after they click the 'submit' input button. That is - not to serve up a different HTML file, but to change, say one of the divs in my home.html file, or add a new div. At the moment I've thought of two possiblities:

However, I have a feeling that there's a simpler way to do this, within the same file (home.html) using a method of Flask. I've spent a while searching through the docs and SO, without success.

class UI:
    def __init__(self):
        app = Flask(__name__)

        @app.route('/')
        def home():
            return render_template("home.html")

        @app.route('/', methods=['POST', 'GET'])
        def update_info():
            foo = request.form['foo']
            bar = request.form['bar']
            if request.method == 'POST':
                 return render_template("new_home.html")

        app.debug = True
        app.run()

Upvotes: 0

Views: 1785

Answers (1)

Allie Fitter
Allie Fitter

Reputation: 1807

A JS script, with onclick() to change the InnerHTML of one of the divs;

This would be the best option in my opinion. Create a div to contain the message above the form--or anywhere else you'd want it. I would suggest using jQuery for this as it's ajax method is very nice for this type of thing. Return either a success or failure response from your app(setting the status code, so jQuery knows what happened), and use the done promise to display success or the fail promise to display errors.

Upvotes: 2

Related Questions