Skip to content

AttributeError: 'Response' object has no attribute 'template_name' #54

@someidiot

Description

@someidiot

Using django-prometheus 1.0.9 with django 1.11.2.

I have some django-rest-framework views that are being cached with the cache_page decorator like:

@method_decorator(cache_page(settings.CACHE_TIMEOUT_API))
def dispatch(self, *args, **kwargs):
    return super(BaseAPICachedView, self).dispatch(*args, **kwargs)

When there is a cache hit, the django-prometheus middleware fails in middleware.py:135 with AttributeError: 'Response' object has no attribute 'template_name'

def process_template_response(self, request, response):
    responses_by_templatename.labels(str(
        response.template_name)).inc()
    return response

Sure enough, the cached views don't seem to have template_name. I fixed it by checking for the attribute first, not sure if that's the best way but it works for me:

def process_template_response(self, request, response):
    if hasattr(response, 'template_name'):
        responses_by_templatename.labels(str(
            response.template_name)).inc()
    return response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions