title: How to redirect from helper function in Flask?
body: <p>On many forms we have a cancel button. Trying to keep things DRY it seemed that a helper function was in order. The function if_form_cancel() would check to see if the cancel button had been clicked and then redirect accordingly (with destination passed to the function). However, the redirect is ignored when we use a helper function (it works just fine in-line).</p>
  
  <p>view.py</p>
  
  <pre><code>from helper.py import if_form_cancel
  ...
  
  def index():
      return render_template('index.html')
  
  def edit():
      ...
      form = EditForm(obj=user, csrf_enabled=False)
      if request.method == 'POST':
          if_form_cancel(url_for('.index')) 
          if form.validate():
              ...
              return redirect(url_for('.index'))
          return render_template('edit.html')
  </code></pre>
  
  <p>helper.py</p>
  
  <pre><code>from flask import request, redirect, flash
  
  def if_form_cancel(destination='/'):
      try:
          if request.form['cancel']:
              flash(u'Operation canceled at user request')
              return redirect(destination)
      except KeyError:
          pass
  </code></pre>
  
  <p>edit.html</p>
  
  <pre><code>....
  &lt;input type="submit" name="submit" value="Submit" /&gt;
  &lt;input type="submit" name="cancel" value="Cancel" /&gt;
  ....
  </code></pre>
  
  <p>Result if cancel button is clicked: helper function is called, correct message is flashed, but redirect does not occur. Is it possible to redirect from a helper function like this? If so how? What are we doing wrong?</p>