# Buggy code
def url_for(self, view_name, **kwargs):
    """
    Same as :func:`sanic.Sanic.url_for`, but automatically determine
    `scheme` and `netloc` base on the request. Since this method is aiming
    to generate correct schema & netloc, `_external` is implied.

    :param kwargs: takes same parameters as in :func:`sanic.Sanic.url_for`
    :return: an absolute url to the given view
    :rtype: str
    """
    # Full URL SERVER_NAME can only be handled in app.url_for
    if "//" in self.app.config.SERVER_NAME:
        return self.app.url_for(view_name, _external=True, **kwargs)

    scheme = self.scheme
    host = self.server_name
    port = self.server_port

    if (scheme.lower() in ("http", "ws") and port == 80) or (
        scheme.lower() in ("https", "wss") and port == 443
    ):
        netloc = host
    else:
        netloc = "{}:{}".format(host, port)

    return self.app.url_for(
        view_name, _external=True, _scheme=scheme, _server=netloc, **kwargs
    )


# Traceback (AttributeError)
Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/sanic/app.py", line 942, in handle_request
response = await response
File "/app/tsauth/views/activate.py", line 41, in init_activate
request=request)
File "/usr/local/lib/python3.7/site-packages/mako/template.py", line 476, in render
return runtime._render(self, self.callable_, args, data)
File "/usr/local/lib/python3.7/site-packages/mako/runtime.py", line 883, in _render
**_kwargs_for_callable(callable_, data)
File "/usr/local/lib/python3.7/site-packages/mako/runtime.py", line 920, in _render_context
_exec_template(inherit, lclcontext, args=args, kwargs=kwargs)
File "/usr/local/lib/python3.7/site-packages/mako/runtime.py", line 947, in _exec_template
callable_(context, *args, **kwargs)
File "transaction_mako", line 60, in render_body
File "/usr/local/lib/python3.7/site-packages/sanic/request.py", line 522, in url_for
if "//" in self.app.config.SERVER_NAME:
File "/usr/local/lib/python3.7/site-packages/sanic/config.py", line 54, in __getattr__
raise AttributeError("Config has no '{}'".format(ke.args[0]))
AttributeError: Config has no 'SERVER_NAME'
[2019-10-23 12:38:09 +0000] - (sanic.access)[INFO][<redacted>:33328]: GET <redacted>  500 144
10/23/2019 12:38:09 PM ERROR Exception occurred while handling uri: '<redacted>'
Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/sanic/config.py", line 52, in __getattr__
return self[attr]
KeyError: 'SERVER_NAME'

# Fixed code
def url_for(self, view_name, **kwargs):
    """
    Same as :func:`sanic.Sanic.url_for`, but automatically determine
    `scheme` and `netloc` base on the request. Since this method is aiming
    to generate correct schema & netloc, `_external` is implied.

    :param kwargs: takes same parameters as in :func:`sanic.Sanic.url_for`
    :return: an absolute url to the given view
    :rtype: str
    """
    # Full URL SERVER_NAME can only be handled in app.url_for
    try:
        if "//" in self.app.config.SERVER_NAME:
            return self.app.url_for(view_name, _external=True, **kwargs)
    except AttributeError:
        pass

    scheme = self.scheme
    host = self.server_name
    port = self.server_port

    if (scheme.lower() in ("http", "ws") and port == 80) or (
        scheme.lower() in ("https", "wss") and port == 443
    ):
        netloc = host
    else:
        netloc = "{}:{}".format(host, port)

    return self.app.url_for(
        view_name, _external=True, _scheme=scheme, _server=netloc, **kwargs
    )