Sentry-python: DjangoIntegration conflict with MiddlewareMixin & TemplateView

Created on 30 Mar 2020  Â·  10Comments  Â·  Source: getsentry/sentry-python

django version: 3.0
sentry-sdk version: 0.14.3

My project has a middleware using Django's django.utils.deprecation.MiddlewareMixin. Visiting a view which subclasses django.views.generic.TemplateView while Sentry is active results in the following exception:

AttributeError: 'function' object has no attribute '__self__'
  File "django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "django/core/handlers/base.py", line 196, in _get_response
    middleware_method.__self__.__class__.__name__,

The following classes & url config should be sufficient to demonstrate the problem:

from django.utils.deprecation import MiddlewareMixin
from django.views.generic import TemplateView

# ...in myapp.middleware.py
class DemonstratesConflictMiddleware(MiddlewareMixin):
    def process_template_response(self, request, response):
        return response

# ...in myapp.views.py
class DemonstratesConflictView(TemplateView): 
    template_name = "index.html"


# ...in urls.py
import myapp.views
urlpatterns += [
    path('/', myapp.views.DemonstratesConflictView.as_view(), name='throws-exception'),
]

# ... in settings.py

MIDDLEWARE += ['myapp.middleware.DemonstratesConflictMiddleware']

sentry_sdk.init(
    dsn="OMITTED",
    integrations=[DjangoIntegration()],
)

bug needs-information

All 10 comments

Sorry for the slow response, I will look into this now

Sorry, can't reproduce. Can you provide a template that exhibits this issue?

Closing due to lack of activity, please respond so we can continue investigating

@untitaker I've got the same error. With custom middleware with process_template_response and sentry enabled

Can you provide a minimal self-contained example so I can reproduce this? I
already tried it once and clearly there is more to it than what is
described in the issue.

On Fri, Jul 31, 2020, 06:15 Mikhail Podgurskiy notifications@github.com
wrote:

@untitaker https://github.com/untitaker I've got the same error. With
custom middleware with process_template_response and sentry enabled

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/getsentry/sentry-python/issues/661#issuecomment-666906913,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/AAGMPRJ7MOBCF2YUJISBQ2TR6JANDANCNFSM4LW27BKA
.

Here is the simple gist. I've added only new style custom middleware with process_template_response method

git clone https://gist.github.com/8760daf908e58a600af4b5ad623fd3e5.git sentry-bug
cd sentry-bug
poetry install
poetry run python ./manage.py runserver

Access to http://127.0.0.1:8000/

Django version 3.1rc1, using settings 'settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
Internal Server Error: /login/
Traceback (most recent call last):
  File "/tmp/sentry-bug/.venv/lib/python3.6/site-packages/django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "/tmp/sentry-bug/.venv/lib/python3.6/site-packages/django/core/handlers/base.py", line 198, in _get_response
    middleware_method.__self__.__class__.__name__,
AttributeError: 'function' object has no attribute '__self__'

Without sentry.init in settings.py the sample works correctly and shows login page

excellent, thank you

yes, so the crucial information missing from op was that this can only be reproduced using 3.1, which is not what you get with pip install django. I've posted a PR. Why do people use pre-release versions?

0.16.3 is out which fixes the problem.

Not fixed in 0.16.2

Was this page helpful?
0 / 5 - 0 ratings

Related issues

CasperCL picture CasperCL  Â·  7Comments

miracle2k picture miracle2k  Â·  6Comments

hyperknot picture hyperknot  Â·  4Comments

thoongnv picture thoongnv  Â·  3Comments

robd003 picture robd003  Â·  5Comments