Django-debug-toolbar: Barra de ferramentas não exibida para solicitações POST

Criado em 4 out. 2017  ·  6Comentários  ·  Fonte: jazzband/django-debug-toolbar

Olá, estou tentando depurar um problema em que POST solicitações não estão fazendo com que a barra de ferramentas apareça; para qualquer outra solicitação GET , a barra de ferramentas de depuração mostra muito bem, não parece ser um problema para outros usuários, então estou me perguntando se algo está fazendo com que a barra de ferramentas não seja acionada ou se este não é um recurso compatível.

Isso só acontece a pedido feito para ViewSets , ModelViewSet do framework django rest, parece funcionar muito bem, como posso descobrir o que está errado?

Comentários muito úteis

Aqui está meu middleware para solucionar esse 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

Certifique-se de adicionar esse middleware _antes_ do middleware do Django Debug Toolbar.

Todos 6 comentários

Eu começaria aqui https://github.com/jazzband/django-debug-toolbar/blob/master/debug_toolbar/middleware.py#L58 , respectivamente com SHOW_TOOLBAR_CALLBACK .

Não sei por que as solicitações POST seriam ignoradas pela barra de ferramentas?

Aqui está meu middleware para solucionar esse 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

Certifique-se de adicionar esse middleware _antes_ do middleware do Django Debug Toolbar.

Não tenho certeza se está tudo bem ou não. Estou tentando usar várias maneiras de capturar a solicitação de postagem, mas ainda não consigo, isso significa que se você usar a solicitação no site com o método 'get', obviamente não há problema. O propósito que eu uso o Django é para desenvolver o servidor para o cliente móvel e a maior parte do método de solicitação é 'POST'. Espero que vocês possam me ajudar, obrigado.

O que funcionou para mim (em combinação com a API navegável do Django REST Framework) é este django-debug-toolbar-history .

django-debug-toolbar-history também funcionou para mim usando as páginas navegáveis ​​do desenvolvedor DRF.

Isso me permitiu rastrear as consultas SQL usadas durante um POST.

Fechando quando o painel de história foi lançado. Também confirmei que, para solicitações POST que retornam e resposta HTML, a barra de ferramentas de estreia aparece.

Se você atualizar para a versão mais recente e continuar a ter um bug, reabra com novos detalhes. Obrigado.

Esta página foi útil?
0 / 5 - 0 avaliações