old_contents: from flask import request, url_for, redirect
  from urllib.parse import urlparse, urljoin
  
  
  def is_safe_url(target):
      ref_url = urlparse(request.host_url)
      test_url = urlparse(urljoin(request.host_url, target))
      return test_url.scheme in ('http', 'https') and \
          ref_url.netloc == test_url.netloc
  
  
  def redirect_back(default='home.home', **values):
      for target in request.values.get('next'), request.referrer:
          if not target:
              continue
          if is_safe_url(target):
              return redirect(target)
      return redirect(url_for(default, **values))
  
new_contents: from flask import request, url_for, redirect as flask_redirect
  from urllib.parse import urlparse, urljoin
  
  
  def is_safe_url(target):
      ref_url = urlparse(request.host_url)
      test_url = urlparse(urljoin(request.host_url, target))
      return test_url.scheme in ('http', 'https') and \
          ref_url.netloc == test_url.netloc
  
  
  def redirect_back(default='home.home', **values):
      for target in request.values.get('next'), request.referrer:
          if not target:
              continue
          if is_safe_url(target):
              return flask_redirect(target)
      return flask_redirect(url_for(default, **values))
  
subject: Use the flask redirect.