# Buggy code
def get_base_url(request=None):
    base_url = getattr(
        settings, "WAGTAILAPI_BASE_URL", request.site.root_url if request else None
    )

    if base_url:
        # We only want the scheme and netloc
        base_url_parsed = urlparse(base_url)

        return base_url_parsed.scheme + "://" + base_url_parsed.netloc


# Traceback (AttributeError)
Internal Server Error: /cms/api/v2beta/pages/
Traceback (most recent call last):
File "/Users/thomas/virtualenvs/djangoenv/lib/python3.5/site-packages/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/Users/thomas/virtualenvs/djangoenv/lib/python3.5/site-packages/django/core/handlers/base.py", line 249, in _legacy_get_response
response = self._get_response(request)
File "/Users/thomas/virtualenvs/djangoenv/lib/python3.5/site-packages/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/Users/thomas/virtualenvs/djangoenv/lib/python3.5/site-packages/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/thomas/virtualenvs/djangoenv/lib/python3.5/site-packages/django/views/decorators/cache.py", line 43, in _cache_controlled
response = viewfunc(request, *args, **kw)
File "/Users/thomas/virtualenvs/djangoenv/lib/python3.5/site-packages/wagtail/wagtailadmin/urls/__init__.py", line 96, in wrapper
return view_func(request, *args, **kwargs)
File "/Users/thomas/virtualenvs/djangoenv/lib/python3.5/site-packages/wagtail/wagtailadmin/decorators.py", line 31, in decorated_view
return view_func(request, *args, **kwargs)
File "/Users/thomas/virtualenvs/djangoenv/lib/python3.5/site-packages/wagtail/api/v2/router.py", line 65, in wrapped
return func(request, *args, **kwargs)
File "/Users/thomas/virtualenvs/djangoenv/lib/python3.5/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
return view_func(*args, **kwargs)
File "/Users/thomas/virtualenvs/djangoenv/lib/python3.5/site-packages/rest_framework/viewsets.py", line 90, in view
return self.dispatch(request, *args, **kwargs)
File "/Users/thomas/virtualenvs/djangoenv/lib/python3.5/site-packages/rest_framework/views.py", line 489, in dispatch
response = self.handle_exception(exc)
File "/Users/thomas/virtualenvs/djangoenv/lib/python3.5/site-packages/wagtail/api/v2/endpoints.py", line 95, in handle_exception
return super(BaseAPIEndpoint, self).handle_exception(exc)
File "/Users/thomas/virtualenvs/djangoenv/lib/python3.5/site-packages/rest_framework/views.py", line 449, in handle_exception
self.raise_uncaught_exception(exc)
File "/Users/thomas/virtualenvs/djangoenv/lib/python3.5/site-packages/rest_framework/views.py", line 486, in dispatch
response = handler(request, *args, **kwargs)
File "/Users/thomas/virtualenvs/djangoenv/lib/python3.5/site-packages/wagtail/wagtailadmin/api/endpoints.py", line 93, in listing_view
response = super(PagesAdminAPIEndpoint, self).listing_view(request)
File "/Users/thomas/virtualenvs/djangoenv/lib/python3.5/site-packages/wagtail/api/v2/endpoints.py", line 81, in listing_view
return self.get_paginated_response(serializer.data)
File "/Users/thomas/virtualenvs/djangoenv/lib/python3.5/site-packages/rest_framework/serializers.py", line 739, in data
ret = super(ListSerializer, self).data
File "/Users/thomas/virtualenvs/djangoenv/lib/python3.5/site-packages/rest_framework/serializers.py", line 263, in data
self._data = self.to_representation(self.instance)
File "/Users/thomas/virtualenvs/djangoenv/lib/python3.5/site-packages/rest_framework/serializers.py", line 657, in to_representation
self.child.to_representation(item) for item in iterable
File "/Users/thomas/virtualenvs/djangoenv/lib/python3.5/site-packages/rest_framework/serializers.py", line 657, in <listcomp>
self.child.to_representation(item) for item in iterable
File "/Users/thomas/virtualenvs/djangoenv/lib/python3.5/site-packages/wagtail/api/v2/serializers.py", line 275, in to_representation
attribute = field.get_attribute(instance)
File "/Users/thomas/virtualenvs/djangoenv/lib/python3.5/site-packages/wagtail/api/v2/serializers.py", line 47, in get_attribute
url = get_object_detail_url(self.context, type(instance), instance.pk)
File "/Users/thomas/virtualenvs/djangoenv/lib/python3.5/site-packages/wagtail/api/v2/serializers.py", line 20, in get_object_detail_url
return get_full_url(context['request'], url_path)
File "/Users/thomas/virtualenvs/djangoenv/lib/python3.5/site-packages/wagtail/api/v2/utils.py", line 25, in get_full_url
base_url = get_base_url(request) or ''
File "/Users/thomas/virtualenvs/djangoenv/lib/python3.5/site-packages/wagtail/api/v2/utils.py", line 15, in get_base_url
base_url = getattr(settings, 'WAGTAILAPI_BASE_URL', request.site.root_url if request else None)
AttributeError: 'NoneType' object has no attribute 'root_url'

# Fixed code
def get_base_url(request=None):
    base_url = getattr(
        settings,
        "WAGTAILAPI_BASE_URL",
        request.site.root_url if request and request.site else None,
    )

    if base_url:
        # We only want the scheme and netloc
        base_url_parsed = urlparse(base_url)

        return base_url_parsed.scheme + "://" + base_url_parsed.netloc