Django-debug-toolbar: شريط الأدوات لا يظهر لطلبات POST

تم إنشاؤها على ٤ أكتوبر ٢٠١٧  ·  6تعليقات  ·  مصدر: jazzband/django-debug-toolbar

مرحبًا ، أحاول تصحيح مشكلة حيث لا تتسبب طلبات POST ظهور شريط الأدوات ، وبالنسبة لأي طلب GET ، يظهر شريط تصحيح الأخطاء على ما يرام ، ولا يبدو أنه يمثل مشكلة للمستخدمين الآخرين ، لذلك أتساءل عما إذا كان هناك شيء ما يتسبب في عدم تشغيل شريط الأدوات أو إذا لم تكن هذه ميزة مدعومة.

يحدث هذا فقط عند الطلب المقدم إلى ViewSets ، ModelViewSet من إطار django rest ، يبدو أنه يعمل بشكل جيد ، كيف يمكنني معرفة الخطأ؟

التعليق الأكثر فائدة

إليك برمجتي الوسيطة للتغلب على هذه المشكلة:

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

تأكد من إضافة هذا البرنامج الوسيط _bقبل_ البرنامج الوسيط الخاص بـ Django Debug Toolbar.

ال 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

تأكد من إضافة هذا البرنامج الوسيط _bقبل_ البرنامج الوسيط الخاص بـ Django Debug Toolbar.

لست متأكدًا من أنه بخير أم لا. أحاول استخدام العديد من الطرق لالتقاط طلب النشر ولكن ما زلت غير قادر ، هذا يعني أنه إذا كنت تستخدم الطلب على موقع الويب باستخدام طريقة "get" ، فمن الواضح أنها ليست مشكلة. الغرض من استخدام Django هو تطوير الخادم للعميل المحمول ومعظم طرق الطلب هي "POST". أتمنى أن تساعدوني يا رفاق ، أقدر.

ما نجح بالنسبة لي (بالاشتراك مع واجهة برمجة التطبيقات القابلة للتصفح لإطار عمل Django REST) ​​هو سجل شريط أدوات django-debug .

نجح django-debug-toolbar-history أيضًا بالنسبة لي باستخدام صفحات مطور DRF القابلة للتصفح.

سمح لي هذا بتتبع استعلامات SQL المستخدمة أثناء POST.

يتم الإغلاق نظرًا لإصدار لوحة المحفوظات. لقد أكدت أيضًا أنه بالنسبة لطلبات POST التي تعود واستجابة HTML ، يظهر شريط الأدوات لأول مرة.

إذا قمت بالتحديث إلى أحدث إصدار واستمرت في مواجهة خطأ ، فيرجى إعادة الفتح بتفاصيل جديدة. شكرا.

هل كانت هذه الصفحة مفيدة؟
0 / 5 - 0 التقييمات