8

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.

4
  • 1
    What is rendered for your form action on the page?
    – mr.freeze
    Oct 19, 2017 at 3:24
  • The first time it prints out statements to the console log that are in the algorithm and updates the webpage after the POST request is completed. Any other time it skips the algorithm and immediately states a POST request has been completed nor does it update the webpage. Oct 19, 2017 at 4:48
  • 1
    It is not clear to me. Are you trying to view all searched cLinkName and lLinkName in the table?
    – arshovon
    Oct 19, 2017 at 5:35
  • No, they both are just supposed to get updated based on what I input into SearchForm Oct 19, 2017 at 6:27

1 Answer 1

11

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

1
  • 2
    I currently do this but it adds the same page to the browser history, so when you click back you will return to the same page. Any ideas on how to resolve this?
    – scrollout
    Apr 25, 2020 at 1:40

Not the answer you're looking for? Browse other questions tagged or ask your own question.