Redirect to other view after submitting form

tommy

I have a survey form. After submitting the form, I'd like to handle saving the data then redirect to a "sucess" view. I'm using the following code right now, but it just stays on the current url, while I'd like to go to /success. How can I do this?

@app.route('/surveytest', methods=['GET', 'POST'])
def surveytest():
    if request.method == 'GET':
        return render_template('test.html', title='Survey Test', year=datetime.now().year, message='This is the survey page.')
    elif request.method == 'POST':
        name = request.form['name']
        address = request.form['address']
        phone = request.form['phone']
        email = request.form['email']
        company = request.form['company']
        return render_template('success.html', name=name, address=address, phone = phone, email = email, company = company)
davidism

You have the right goal: it's good to redirect after handling form data. Rather than returning render_template again, use redirect instead.

from flask import redirect, url_for, survey_id

@app.route('/success/<int:result_id>')
def success(result_id):
     # replace this with a query from whatever database you're using
     result = get_result_from_database(result_id)
     # access the result in the tempalte, for example {{ result.name }}
     return render_template('success.html', result=result)

@app.route('/survey', methods=["GET", "POST"])
def survey():
    if request.method == 'POST':
        # replace this with an insert into whatever database you're using
        result = store_result_in_database(request.args)
        return redirect(url_for('success', result_id=result.id))

    # don't need to test request.method == 'GET'
    return render_template('survey.html')

The redirect will be handled by the user's browser, and the new page at the new url will be loaded, rather than rendering a different template at the same url.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Redirect to other view after submitting form

From Dev

redirect List<Data> to a view after user has submitting a form

From Dev

Redirect after submitting a form (HTML, PHP)

From Dev

How to redirect after submitting form in php

From Dev

redirect to another page after submitting a form

From Dev

How to redirect to List View Page in PHP CodeIgniter using parameters after submitting a form?

From Dev

How to redirect on submitting a form?

From Dev

How to prevent redirect after submitting data through a form

From Dev

Redirect to contacts page after submitting form in laravel 4

From Dev

How to update parent view after submitting a form through a partial view?

From Dev

Form not submitting after form validation

From Dev

After submitting a remote form with Rails, how do I redirect the user to another page?

From Dev

After submitting a rails form via AJAX, local server shows successful GET but browser does not redirect

From Dev

Disable hyperlink after submitting the form

From Dev

AJAX not submitting the form after validation

From Dev

Empty modelAttribute after submitting a form

From Dev

Submitting form after validating inputs

From Dev

Django redirect to index view with correct URL after form submission

From Dev

Redirect to %5c after submitting in django

From Dev

Redirect the WordPress plugin after submitting button is clicked

From Dev

Refresh Form page after submitting the form?

From Dev

Form disappers after submitting form using ajax

From Dev

Redirect After Form Submit

From Dev

form_for submitting other submits too

From Dev

I want to redirect the current page after submitting the form....it should return the page from where request is send using php

From Dev

rails redirect to edit page when submitting the form with the params of saved form

From Dev

Submitting data from a form to django view

From Dev

Rails submitting a form through ajax and updating the view

From Dev

Return other View without Redirect

Related Related

  1. 1

    Redirect to other view after submitting form

  2. 2

    redirect List<Data> to a view after user has submitting a form

  3. 3

    Redirect after submitting a form (HTML, PHP)

  4. 4

    How to redirect after submitting form in php

  5. 5

    redirect to another page after submitting a form

  6. 6

    How to redirect to List View Page in PHP CodeIgniter using parameters after submitting a form?

  7. 7

    How to redirect on submitting a form?

  8. 8

    How to prevent redirect after submitting data through a form

  9. 9

    Redirect to contacts page after submitting form in laravel 4

  10. 10

    How to update parent view after submitting a form through a partial view?

  11. 11

    Form not submitting after form validation

  12. 12

    After submitting a remote form with Rails, how do I redirect the user to another page?

  13. 13

    After submitting a rails form via AJAX, local server shows successful GET but browser does not redirect

  14. 14

    Disable hyperlink after submitting the form

  15. 15

    AJAX not submitting the form after validation

  16. 16

    Empty modelAttribute after submitting a form

  17. 17

    Submitting form after validating inputs

  18. 18

    Django redirect to index view with correct URL after form submission

  19. 19

    Redirect to %5c after submitting in django

  20. 20

    Redirect the WordPress plugin after submitting button is clicked

  21. 21

    Refresh Form page after submitting the form?

  22. 22

    Form disappers after submitting form using ajax

  23. 23

    Redirect After Form Submit

  24. 24

    form_for submitting other submits too

  25. 25

    I want to redirect the current page after submitting the form....it should return the page from where request is send using php

  26. 26

    rails redirect to edit page when submitting the form with the params of saved form

  27. 27

    Submitting data from a form to django view

  28. 28

    Rails submitting a form through ajax and updating the view

  29. 29

    Return other View without Redirect

HotTag

Archive