Flask CGI app issues with print statement
<p>I have a simple flask application deployed using CGI+Apache running on a shared hosting server.</p>

<p>The app runs Flask 0.11.1 on Python 2.6 along with Flask-Mail 0.9.1.</p>

<p>One of the pages in the app has a contact form that sends an email and redirects back to the same page.</p>

<p>The contact form has a POST action to '/sendmail' that is defined in Flask controller as follows - </p>

<pre><code>@app.route("/sendmail", methods=['GET','POST'])
def send_mail():
    print "Sending Email"
    mail = SendMail(app)
    mail.send_mail(request.form['name'], request.form['mail'], request.form['phoneNo'], request.form['message'])
    return render_template('contact.html')
</code></pre>

<p>Here's the issue - </p>

<ol>
<li>With the code above, the app sends me an email successfully, however then gives an error '/sendmail' not found. The template fails to render.</li>
<li>If I remove the print statement from the snippet, the app renders contact.html successfully after sending the email.</li>
</ol>

<p>What explains the behaviour of print statement in the snippet? Considering the execution is sequential, shouldn't the block fail at the print statement itself without sending the email instead of failing during rendering the template?</p>

<p>Print statement should not create an error as it is one of the statement like others. Instead since you are not checking for <code>request.method=='POST'</code>, this should create and throw an error in your get request. To redirect to the same page <code>return redirect("/sendmail")</code> Do not forget to import from flask like this <code>from flask import redirect</code></p>

python flask