Django-debug-toolbar: Toolbar not showing for POST requests

Created on 4 Oct 2017  ·  6Comments  ·  Source: jazzband/django-debug-toolbar

Hi, I'm trying to debug a problem where POST requests are not causing the toolbar to show up, for any other GET request, the debug toolbar shows just fine, doesn't seem to be a problem for other users so I'm wondering if something is causing the toolbar not to trigger or if this is not a supported feature.

This only happens on request made to ViewSets, ModelViewSet from django rest framework, seems to work just fine, how can I figure out what's wrong?

Most helpful comment

Here is my middleware to work around this issue:

def Fix_XMLHttpRequest(get_response):
    def middleware(request):
        # When `POST`ing data from the DRF browsable API, it will set the
        # header `X_REQUESTED_WITH: XMLHttpRequest`. (Not sure if the browser
        # adds that header of if DRF's front-end does, but that doesn't matter,
        # the header is there.) It's a little contradictory that the header is
        # there since the browser also requests that the response be 'text/html'.
        # Anyway, that's fine except for one thing. Django Debug Toolbar will
        # not show the debug toolbar whenever that header is set on the request!
        #
        # See:
        #   - https://github.com/jazzband/django-debug-toolbar/blob/c201ce34cea3ba4ce99d1642db17f2cb31c6204e/debug_toolbar/middleware.py#L59
        #   - https://docs.djangoproject.com/en/2.1/_modules/django/http/request/#HttpRequest.is_ajax
        #
        # My workaround is to remove that header whenever the request explicitly
        # asks for 'text/html'. Again, in my opinion, that header doesn't really
        # make sense anyway when the browser wants to receive HTML back.
        #
        # This makes is so that when you POST data from the DRF browsable API,
        # you can still see the Django Debug Toolbar. It maintains the desired
        # behavior of _not_ showing the toolbar when you POST from Javascript
        # to obtain JSON data.

        if 'text/html' in request.META.get('HTTP_ACCEPT', ''):
            if request.META.get('HTTP_X_REQUESTED_WITH', '') == 'XMLHttpRequest':
                del request.META['HTTP_X_REQUESTED_WITH']

        response = get_response(request)

        return response

    return middleware

Be sure to add that middleware _before_ your Django Debug Toolbar's middleware.

All 6 comments

I'd start here https://github.com/jazzband/django-debug-toolbar/blob/master/debug_toolbar/middleware.py#L58 , respectively with the SHOW_TOOLBAR_CALLBACK.

I'm not sure why POST requests would be ignored by the toolbar?

Here is my middleware to work around this issue:

def Fix_XMLHttpRequest(get_response):
    def middleware(request):
        # When `POST`ing data from the DRF browsable API, it will set the
        # header `X_REQUESTED_WITH: XMLHttpRequest`. (Not sure if the browser
        # adds that header of if DRF's front-end does, but that doesn't matter,
        # the header is there.) It's a little contradictory that the header is
        # there since the browser also requests that the response be 'text/html'.
        # Anyway, that's fine except for one thing. Django Debug Toolbar will
        # not show the debug toolbar whenever that header is set on the request!
        #
        # See:
        #   - https://github.com/jazzband/django-debug-toolbar/blob/c201ce34cea3ba4ce99d1642db17f2cb31c6204e/debug_toolbar/middleware.py#L59
        #   - https://docs.djangoproject.com/en/2.1/_modules/django/http/request/#HttpRequest.is_ajax
        #
        # My workaround is to remove that header whenever the request explicitly
        # asks for 'text/html'. Again, in my opinion, that header doesn't really
        # make sense anyway when the browser wants to receive HTML back.
        #
        # This makes is so that when you POST data from the DRF browsable API,
        # you can still see the Django Debug Toolbar. It maintains the desired
        # behavior of _not_ showing the toolbar when you POST from Javascript
        # to obtain JSON data.

        if 'text/html' in request.META.get('HTTP_ACCEPT', ''):
            if request.META.get('HTTP_X_REQUESTED_WITH', '') == 'XMLHttpRequest':
                del request.META['HTTP_X_REQUESTED_WITH']

        response = get_response(request)

        return response

    return middleware

Be sure to add that middleware _before_ your Django Debug Toolbar's middleware.

I'm not sure it is okay or not. I'm trying use many ways to capture the post request but still cant not, that means if you use request on website with 'get' method , obviously it's no problem . The purpose I use Django is for developing the server for mobile client and most of request method is 'POST' . Hope you guys can help me , appreciate.

What worked for me (in combination with browsable API of the Django REST Framework) is this django-debug-toolbar-history.

django-debug-toolbar-history also worked for me using the browsable DRF developer pages.

This allowed me to track the SQL queries used during a POST.

Closing as the history panel has been released. I also confirmed that for POST requests that return and HTML response, the debut toolbar shows up.

If you update to the latest release and continue to experience a bug please reopen with new details. Thanks.

Was this page helpful?
0 / 5 - 0 ratings