code: def get_next_url(referrer=False, external=False, session=False, default=__marker):
      
      if session:
          next_url = request_session.pop('next', None) or request.args.get('next', '')
      else:
          next_url = request.args.get('next', '')
      if next_url and not external:
          next_url = __clean_external_url(next_url)
      if next_url:
          return next_url
  
      if default is __marker:
          usedefault = False
      else:
          usedefault = True
  
      if referrer and request.referrer:
          if external:
              return request.referrer
          else:
              return __clean_external_url(request.referrer) or (default if usedefault else __index_url())
      else:
          return default if usedefault else __index_url()
docs: Get the next URL to redirect to. Don't return external URLs unless
      explicitly asked for. This is to protect the site from being an unwitting
      redirector to external URLs. Subdomains are okay, however.
  
      This function looks for a ``next`` parameter in the request or in the session
      (depending on whether parameter ``session`` is True). If no ``next`` is present,
      it checks the referrer (if enabled), and finally returns either the provided
      default (which can be any value including ``None``) or the script root
      (typically ``/``).