# Buggy code
def get_extra_context(self, request, instance):
    related_paths = []

    # If tracing a PathEndpoint, locate the CablePath (if one exists) by its origin
    if isinstance(instance, PathEndpoint):
        path = instance._path

    # Otherwise, find all CablePaths which traverse the specified object
    else:
        related_paths = CablePath.objects.filter(path__contains=obj).prefetch_related(
            "origin"
        )
        # Check for specification of a particular path (when tracing pass-through ports)
        try:
            path_id = int(request.GET.get("cablepath_id"))
        except TypeError:
            path_id = None
        if path_id in list(related_paths.values_list("pk", flat=True)):
            path = CablePath.objects.get(pk=path_id)
        else:
            path = related_paths.first()

    return {
        "path": path,
        "related_paths": related_paths,
        "total_length": path.get_total_length() if path else None,
    }


# Traceback (NameError)
Traceback (most recent call last):
File "/usr/local/netbox/venv/lib64/python3.6/site-packages/django/core/handlers/exception.py", line 47, in inner
response = get_response(request)
File "/usr/local/netbox/venv/lib64/python3.6/site-packages/django/core/handlers/base.py", line 179, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/usr/local/netbox/venv/lib64/python3.6/site-packages/django/views/generic/base.py", line 70, in view
return self.dispatch(request, *args, **kwargs)
File "/usr/local/netbox/netbox/dcim/views.py", line 2068, in dispatch
return super().dispatch(request, *args, **kwargs)
File "/usr/local/netbox/netbox/utilities/views.py", line 91, in dispatch
return super().dispatch(request, *args, **kwargs)
File "/usr/local/netbox/venv/lib64/python3.6/site-packages/django/views/generic/base.py", line 98, in dispatch
return handler(request, *args, **kwargs)
File "/usr/local/netbox/netbox/netbox/views/generic.py", line 70, in get
**self.get_extra_context(request, instance),
File "/usr/local/netbox/netbox/dcim/views.py", line 2079, in get_extra_context
related_paths = CablePath.objects.filter(path__contains=obj).prefetch_related('origin')

Exception Type: NameError at /dcim/rear-ports/3950/trace/ Exception Value: name 'obj' is not defined

# Fixed code
def get_extra_context(self, request, instance):
    related_paths = []

    # If tracing a PathEndpoint, locate the CablePath (if one exists) by its origin
    if isinstance(instance, PathEndpoint):
        path = instance._path

    # Otherwise, find all CablePaths which traverse the specified object
    else:
        related_paths = CablePath.objects.filter(
            path__contains=instance
        ).prefetch_related("origin")
        # Check for specification of a particular path (when tracing pass-through ports)
        try:
            path_id = int(request.GET.get("cablepath_id"))
        except TypeError:
            path_id = None
        if path_id in list(related_paths.values_list("pk", flat=True)):
            path = CablePath.objects.get(pk=path_id)
        else:
            path = related_paths.first()

    return {
        "path": path,
        "related_paths": related_paths,
        "total_length": path.get_total_length() if path else None,
    }