Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AttributeError: This StreamingHttpResponse instance has no content attribute. Use streaming_content instead. #1945

Closed
danjac opened this issue Jul 5, 2024 · 5 comments · Fixed by #1946

Comments

@danjac
Copy link
Contributor

danjac commented Jul 5, 2024

This appears to happen when returning a FileResponse. The issue looks like a regression in 4.4.3.

django==5.0.6
django-debug-toolbar==4.4.3
File "/my-project/.venv/lib/python3.12/site-packages/django/core/handlers/exception.py", line 140, in response_for_exception
    response = handle_uncaught_exception(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/my-project/.venv/lib/python3.12/site-packages/django/core/handlers/exception.py", line 181, in handle_uncaught_exception
    return debug.technical_500_response(request, *exc_info)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/my-project/.venv/lib/python3.12/site-packages/django_extensions/management/technical_response.py", line 41, in null_technical_500_response
    raise exc_value
  File "/my-project/.venv/lib/python3.12/site-packages/django/core/handlers/exception.py", line 55, in inner
    response = get_response(request)
               ^^^^^^^^^^^^^^^^^^^^^
  File "/my-project/.venv/lib/python3.12/site-packages/debug_toolbar/middleware.py", line 94, in __call__
    panel.generate_stats(request, response)
  File "/my-project/.venv/lib/python3.12/site-packages/debug_toolbar/panels/alerts.py", line 142, in generate_stats
    html_content = response.content.decode(response.charset)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/my-project/.venv/lib/python3.12/site-packages/django/http/response.py", line 458, in content
    raise AttributeError(
AttributeError: This StreamingHttpResponse instance has no `content` attribute. Use `streaming_content` instead.
@danjac
Copy link
Contributor Author

danjac commented Jul 5, 2024

Downgrading to 4.4.2 fixes the issue.

@matthiask
Copy link
Member

Thanks for the report. The new alerts panel should have a check for streaming responses. Do you want to submit a fix for this?

@danjac
Copy link
Contributor Author

danjac commented Jul 5, 2024

Not very familiar with the code but presumably a check needed here ?

https://github.com/jazzband/django-debug-toolbar/blob/main/debug_toolbar/panels/alerts.py#L142

def generate_stats(self, request, response):
        html_content = response.content.decode(response.charset)

@matthiask
Copy link
Member

I wondered for a moment what's going on since we are already checking for streaming responses, but that's only when we check if we should insert the toolbar's HTML, not when generating the stats.

https://github.com/jazzband/django-debug-toolbar/blob/c79f249aa9955fb6be2d4363947406ea13d12155/debug_toolbar/middleware.py#L107-L112

So yes, the generate_stats method should probably have similar checks as well and not process streaming responses at all.

I think we would want a unit test for this as well since we didn't catch this problem in time.

@tim-schilling
Copy link
Member

I wonder if we should do something like we're doing with async and have a streaming compatible attribute on the panels that prevents the panel from attempting to process these responses.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants