title: flask - Get request referrer endpoint
body: <p>I need a way to get the request referrer endpoint from Flask. I need it to be able to have the user change language on the website, and then be redirected to that same page with the updated language.</p>
  
  <h2>views.py</h2>
  
  <pre><code>@app.route('/&lt;language&gt;/change')
  def change_language(language):
  
      if language == 'fr':
          _language = 'en'
      elif language == 'en':
          _language = 'fr'
  
      # makes a response and assigns cookie
      resp = make_response(
          redirect(url_for(request.referrer.endpoint, language=_language)))
      resp.set_cookie('lang', _language)
      return resp
  </code></pre>
  
  <p>Let's say the user has clicked "change language" from the login route below:</p>
  
  <pre><code>@bp.route('/&lt;language&gt;/login', methods=["GET", "POST"])
  def login(language):
      return render_template('/auth/login.html')
  </code></pre>
  
  <p>I need them to be redirected here, but with the right language. I left the broken code above (<code>request.referrer.endpoint</code> doesn't exist) to show what I'm ideally trying to do.</p>
  
  <p>How can I do this?</p>