title: Flask/Werkzeug, how to return previous page after login
body: <p>I am using the Flask micro-framework which is based on Werkzeug, which uses Python.</p>
  
  <p>Before each restricted page there is a decorator to ensure the user is logged in, currently returning them to the login page if they are not logged in, like so:</p>
  
  <pre><code># Decorator
  def logged_in(f):
      @wraps(f)
      def decorated_function(*args, **kwargs):
          try:
              if not session['logged_in']:
                  flash('Please log in first...', 'error')
                  return redirect(url_for('login'))
              else:
                  return f(*args, **kwargs)
          except KeyError:
              flash('Please log in first...', 'error')
              return redirect(url_for('login'))
      return decorated_function
  
  
  # Login function
  @app.route('/', methods=['GET', 'POST'])
  def login():
      """Login page."""
      if request.method=='POST':
      ### Checks database, etc. ###
      return render_template('login.jinja2')
  
  
  # Example 'restricted' page
  @app.route('/download_file')
  @logged_in
  def download_file():
      """Function used to send files for download to user."""
      fileid = request.args.get('id', 0)
      ### ... ###
  </code></pre>
  
  <p>After logging in, it needs to return users to the page that took them to the login page.
  It also needs to retain things such as the passed variables (i.e. the entire link basically www.example.com/download_file?id=3 )</p>
  
  <p>Does anyone know how to do this?
  <br /><br /></p>
  
  <p>Thank you for your help :-)</p>