Django-debug-toolbar: Bilah alat tidak muncul untuk permintaan POST

Dibuat pada 4 Okt 2017  ·  6Komentar  ·  Sumber: jazzband/django-debug-toolbar

Hai, Saya mencoba men-debug masalah di mana permintaan POST tidak menyebabkan bilah alat muncul, untuk permintaan GET , bilah alat debug ditampilkan dengan baik, sepertinya tidak menjadi masalah bagi pengguna lain jadi saya ingin tahu apakah ada sesuatu yang menyebabkan bilah alat tidak terpicu atau apakah ini bukan fitur yang didukung.

Ini hanya terjadi atas permintaan yang dibuat ke ViewSets , ModelViewSet dari kerangka istirahat Django, tampaknya berfungsi dengan baik, bagaimana saya bisa mengetahui apa yang salah?

Komentar yang paling membantu

Inilah middleware saya untuk mengatasi masalah ini:

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

Pastikan untuk menambahkan middleware _before_ middleware Django Debug Toolbar Anda.

Semua 6 komentar

Saya akan mulai di sini https://github.com/jazzband/Django-debug-toolbar/blob/master/debug_toolbar/middleware.py#L58 , masing-masing dengan SHOW_TOOLBAR_CALLBACK .

Saya tidak yakin mengapa permintaan POST akan diabaikan oleh bilah alat?

Inilah middleware saya untuk mengatasi masalah ini:

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

Pastikan untuk menambahkan middleware _before_ middleware Django Debug Toolbar Anda.

Saya tidak yakin itu baik-baik saja atau tidak. Saya mencoba menggunakan banyak cara untuk menangkap permintaan posting tetapi masih tidak bisa, itu berarti jika Anda menggunakan permintaan di situs web dengan metode 'get', jelas tidak masalah. Tujuan saya menggunakan Django adalah untuk mengembangkan server untuk klien seluler dan sebagian besar metode permintaan adalah 'POST' . Semoga kalian bisa membantu saya, menghargai.

Apa yang berhasil bagi saya (dalam kombinasi dengan API yang dapat dijelajahi dari Django REST Framework) adalah Django-debug-toolbar-history ini .

Django-debug-toolbar-history juga bekerja untuk saya menggunakan halaman pengembang DRF yang dapat dijelajahi.

Ini memungkinkan saya untuk melacak kueri SQL yang digunakan selama POST.

Menutup karena panel riwayat telah dirilis. Saya juga mengkonfirmasi bahwa untuk permintaan POST yang kembali dan respons HTML, bilah alat debut muncul.

Jika Anda memperbarui ke rilis terbaru dan terus mengalami bug, silakan buka kembali dengan detail baru. Terima kasih.

Apakah halaman ini membantu?
0 / 5 - 0 peringkat