Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Reload Flask-based Webpage after form submission [duplicate]

Tags:

python

flask

Here is my code currently (omitting libraries and other unnecessary parts):

    app = Flask(__name__)

    class SearchForm(Form):
        inputString = TextAreaField('', [validators.DataRequired()])

    @app.route('/')
    def index():
        return render_template('Website.html')

    @app.route('/results', methods=['POST'])
    def results():
        ...
        form = SearchForm(request.form)
        if request.method == 'POST' and form.validate():
            inputString = request.form['inputString']
        ...
        return render_template('Website.html', cLinkName=cLinkName, \
                                               lLinkName=lLinkName)

    if __name__ == '__main__':
        app.run(debug=True, use_reloader=True)

And here is the webpage (showing only the form and the variables that are changed when the SearchForm is submitted):

    <form method=post action="{{url_for('results')}}" style="text-align: center;">
        <input type="text" id="inputString" name="inputString"/>
        <input type=submit value='Search' name='search_btn'>
    </form>
    <table align="center" border="1">
        <tr>
            <td style='text-align: center;'>{{ cLinkName }}</td>
            <td style='text-align: center;'>{{ lLinkName }}</td>
        </tr>
    </table>

Firstly, I have looked at Flask: redirect to same page after form submission and I believe I tried to implement what was accepted as the correct answer, but it did not work for me since I am also trying to submit variables to the updated webpage rather than just refresh the webpage.

I want to be able to submit the webpage's SearchForm and then update my webpage with the cLinkName and lLinkName produced from submitting my SearchForm more than once. Unfortunately my code is only allowing me to submit the SearchForm and then update my webpage with the cLinkName and lLinkName produced from submitting my SearchForm exactly once. And, when I try to reload the webpage I receive a "Continue Form Resubmission" pop-up. So my question is how can I get my webpage to allow me to submit the SearchForm and update itself more than once?

After further research, Clear valid form after it is submitted, I can now ask for GET requests and ensure that I can reload the localhost/results page and submit another form, but it does not run the form through the algorithm again.

like image 541
C. Lightfoot Avatar asked Oct 19 '17 02:10

C. Lightfoot


People also ask

How do I redirect on the same page in Flask?

Another method you can use when performing redirects in Flask is the url_for() function. The way that url_for() works is instead of redirecting based on the string representation of a route, you provide the function name of the route you want to redirect to.

How do I reload a template in Flask?

Flask does not reload the template when it is changed. After you change a template, you need to reload the web app for it to get the new template. hi, thanks for the help.


1 Answers

Added return redirect(url_for('results')) as last line in if request.method == 'POST' and form.validate(): body and everything worked fine.

like image 184
C. Lightfoot Avatar answered Oct 03 '22 12:10

C. Lightfoot