Flask redirect is not working, no error, and db isn't updated

Viewed 431

When submitting my form, the page is not redirected. There are no errors; some information is added to the url, and that's it. The database is not updated afterwards.

`from regist import app
from flask import render_template, redirect, url_for
from regist.models import Applicant
from regist.forms import RegisterForm
from regist import db

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

@app.route('/redirected')
def redirected():
      return render_template('page.html')      

@app.route('/join', methods=['GET','POST'])
def join():
    form=RegisterForm()
    if form.validate_on_submit():
          applicant_to_create=Applicant(name=form.name.data,
                                        surname=form.surname.data,
                                        gender=form.gender.data,
                                        dateOfBirth=form.dateOfBirth.data,
                                        address=form.address.data,
                                        qualifications=form.qualifications.data,
                                        email=form.email.data,
                                        phoneNumber=form.phoneNumber.data)  
          db.session.add(applicant_to_create)
          db.session.commit()   
          return redirect(url_for('redirected'))                     
    return render_template('join.html',form=form)  `

The initial url is: "http://127.0.0.1:5000/join"

After adding some dummy data, it becomes: "http://127.0.0.1:5000/join?search=+%28ru%28rzshrhf&csrf_token=ImIwZWIwODcyNWQ4MWNkYjNjYWJkNjE5NTNkY2MwZWE5Yzk4MjNkOTci.YR4veQ.eyoXQWv4kRShFiZMEbPXoWzsbL0&name=dhss&surname=sdh&gender=M&dateOfBirth=sdthdh&address=dshtes&qualifications=dshdh&email=dhr&phoneNumber=dshd&submit=submit" I intentionally set the date field to accept text.

here is the form html code:

 {%extends 'layout.html' %}
{%block head%}
<title>form</title>
<link rel="stylesheet" href="{{ url_for('static', filename='css/formstyle.css') }}">
{%endblock%}

{%block body%}
<body> 
    
        <h2>Join Us</h2>
        <span id="requiredFieldMessage">fields marked (*) are required</span>
        <form method="POST">
        <!--{{ form.hidden_tag() }}-->  
        <table id="formTable">
            <tr><td class="labelCell">{{ form.name.label()}}</td>   <td class="labelCell">{{ form.surname.label()}}</td></tr>   
            <tr><td>{{ form.name(class="textbox") }}</td><td>{{ form.surname(class="textbox") }}</td></tr>  
            <tr><td class="labelCell">{{ form.gender.label()}}</td> <td class="labelCell">{{ form.dateOfBirth.label()}}</td></tr>   
            <tr><td>{{ form.gender() }}</td>    <td>{{ form.dateOfBirth(class="textbox", placeholder="dd-mm-yyyy") }}</td></tr> 
            <tr><td class="labelCell">{{ form.address.label()}}</td>    <td class="labelCell">{{ form.qualifications.label()}}</td></tr>    
            <tr><td>{{ form.address(class="textbox", rows="5", cols="40", placeholder="Enter four or five line address") }}</td>    <td>{{ form.qualifications(class="textbox", rows="5", cols="40", placeholder="enter at least one certificate") }}</td></tr> 
            <tr><td class="labelCell">{{ form.email.label()}}</td>  <td class="labelCell">{{ form.phoneNumber.label()}}</td></tr>   
            <tr><td>{{ form.email(class="textbox") }}</td>  <td>{{ form.phoneNumber(class="textbox") }}</td></tr>   
            <tr colspan="2"><td><br>{{ form.submit(id="submitButton") }}<br></td></tr>
        </table>
        </form>
{%endblock%}
1 Answers

Assuming that you are using Flask-WTF. Look at what the doc says about validate_on_submit:

Note that you don’t have to pass request.form to Flask-WTF; it will load automatically. And the convenient validate_on_submit will check if it is a POST request and if it is valid.

Source

(emphasis is mine)

Your form should be submitted as POST, not GET. GET forms are rare (mostly search forms because the keywords are propagated in the URL, thus the queries can be shared or bookmarked)

What seems to happen here is that the condition if form.validate_on_submit(): is not true. Once you've fixed your form to use POST, you should display an error message to the user if that condition fails, and ask them to correct their input then, rather than do a silent redirect. It is very irritating to have forms that don't perform validation like they should, and don't communicate clear feedback when errors occur...

All you need to do is add an else clause to your if and display the error messages that should already be available from Flask WTF. When you build a form, test it and make sure it does not accept garbage.

Related