Django-debug-toolbar: POSTリクエストに対してツールバーが表示されない

作成日 2017年10月04日  ·  6コメント  ·  ソース: jazzband/django-debug-toolbar

こんにちは、 POSTリクエストによってツールバーが表示されない問題をデバッグしようとしています。他のGETリクエストの場合、デバッグツールバーは問題なく表示され、表示されないようです。他のユーザーにとっては問題になるので、何かが原因でツールバーがトリガーされないのか、それともこれがサポートされている機能ではないのか疑問に思っています。

これは、django restフレームワークからViewSetsModelViewSetに対して行われた要求でのみ発生し、問題なく機能しているようです。どうすれば問題を特定できますか?

最も参考になるコメント

この問題を回避するためのミドルウェアは次のとおりです。

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

そのミドルウェアをDjangoデバッグツールバーのミドルウェアの_前_に必ず追加してください。

全てのコメント6件

ここから始めますhttps://github.com/jazzband/django-debug-toolbar/blob/master/debug_toolbar/middleware.py#L58 、それぞれSHOW_TOOLBAR_CALLBACK

POSTリクエストがツールバーによって無視される理由がわかりませんか?

この問題を回避するためのミドルウェアは次のとおりです。

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

そのミドルウェアをDjangoデバッグツールバーのミドルウェアの_前_に必ず追加してください。

大丈夫かどうかわかりません。 私は投稿リクエストをキャプチャするために多くの方法を使用しようとしていますが、それでもできません。つまり、ウェブサイトで「get」メソッドを使用してリクエストを使用する場合、明らかに問題はありません。 私がDjangoを使用する目的は、モバイルクライアント用のサーバーを開発することであり、ほとんどのリクエストメソッドは「POST」です。 皆さんが私を助けてくれることを願っています、感謝します。

(Django RESTフレームワークの閲覧可能なAPIと組み合わせて)私のために働いたのは、このdjango-debug-toolbar-historyです。

django-debug-toolbar-historyも、閲覧可能なDRF開発者ページを使用して機能しました。

これにより、POST中に使用されたSQLクエリを追跡できました。

履歴パネルがリリースされたので終了します。 また、返されるPOSTリクエストとHTMLレスポンスの場合、デビューツールバーが表示されることを確認しました。

最新リリースに更新してもバグが引き続き発生する場合は、新しい詳細で再度開いてください。 ありがとう。

このページは役に立ちましたか?
0 / 5 - 0 評価