Django-debug-toolbar: La barra de herramientas no se muestra para las solicitudes POST

Creado en 4 oct. 2017  ·  6Comentarios  ·  Fuente: jazzband/django-debug-toolbar

Hola, estoy tratando de depurar un problema en el que las solicitudes POST no hacen que se muestre la barra de herramientas, para cualquier otra solicitud GET , la barra de herramientas de depuración se muestra bien, no parece ser un problema para otros usuarios, así que me pregunto si algo está causando que la barra de herramientas no se active o si esta no es una función compatible.

Esto solo ocurre a solicitud hecha a ViewSets , ModelViewSet desde django rest framework, parece funcionar bien, ¿cómo puedo averiguar qué está mal?

Comentario más útil

Aquí está mi middleware para solucionar este problema:

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

Asegúrese de agregar ese middleware _antes_ del middleware de su barra de herramientas de depuración de Django.

Todos 6 comentarios

Comenzaría aquí https://github.com/jazzband/django-debug-toolbar/blob/master/debug_toolbar/middleware.py#L58 , respectivamente, con SHOW_TOOLBAR_CALLBACK .

No estoy seguro de por qué la barra de herramientas ignoraría las solicitudes POST.

Aquí está mi middleware para solucionar este problema:

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

Asegúrese de agregar ese middleware _antes_ del middleware de su barra de herramientas de depuración de Django.

No estoy seguro de que esté bien o no. Estoy tratando de usar muchas formas de capturar la solicitud de publicación, pero aún no puedo, eso significa que si usa la solicitud en el sitio web con el método 'get', obviamente no hay problema. El propósito que uso Django es para desarrollar el servidor para el cliente móvil y la mayor parte del método de solicitud es 'POST'. Espero que puedan ayudarme, lo agradezco.

Lo que funcionó para mí (en combinación con la API navegable de Django REST Framework) es este django-debug-toolbar-history .

django-debug-toolbar-history también funcionó para mí usando las páginas de desarrollador de DRF navegables.

Esto me permitió realizar un seguimiento de las consultas SQL utilizadas durante un POST.

Cerrando como se ha liberado el panel de historial. También confirmé que para las solicitudes POST que devuelven y la respuesta HTML, aparece la barra de herramientas de debut.

Si actualiza a la última versión y continúa experimentando un error, vuelva a abrir con nuevos detalles. Gracias.

¿Fue útil esta página
0 / 5 - 0 calificaciones