์๋
ํ์ธ์, POST
์์ฒญ์ผ๋ก ์ธํด ๋๊ตฌ ๋ชจ์์ด ํ์๋์ง ์๋ ๋ฌธ์ ๋ฅผ ๋๋ฒ๊ทธํ๋ ค๊ณ ํฉ๋๋ค. ๋ค๋ฅธ GET
์์ฒญ์ ๋ํด ๋๋ฒ๊ทธ ๋๊ตฌ ๋ชจ์์ด ์ ๋๋ก ํ์๋๊ณ ํ์๋์ง ์๋ ๊ฒ ๊ฐ์ต๋๋ค. ๋ค๋ฅธ ์ฌ์ฉ์์๊ฒ ๋ฌธ์ ๊ฐ ๋ ์ ์์ผ๋ฏ๋ก ๋๊ตฌ ๋ชจ์์ด ํธ๋ฆฌ๊ฑฐ๋์ง ์๊ฑฐ๋ ์ด๊ฒ์ด ์ง์๋๋ ๊ธฐ๋ฅ์ด ์๋์ง ๊ถ๊ธํฉ๋๋ค.
์ด๊ฒ์ ViewSets
, ModelViewSet
django rest ํ๋ ์์ํฌ์์ ์์ฒญํ ๊ฒฝ์ฐ์๋ง ๋ฐ์ํฉ๋๋ค. ์ ๋๋ก ์๋ํ๋ ๊ฒ ๊ฐ์ต๋๋ค. ๋ฌด์์ด ์๋ชป๋์๋์ง ์ด๋ป๊ฒ ์ ์ ์์ต๋๊น?
๊ฐ๊ฐ SHOW_TOOLBAR_CALLBACK
์ฌ์ฉํ์ฌ https://github.com/jazzband/django-debug-toolbar/blob/master/debug_toolbar/middleware.py#L58 ์์ ์์ํ๊ฒ ์ต๋๋ค.
๋๊ตฌ ๋ชจ์์์ 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 Debug Toolbar์ ๋ฏธ๋ค์จ์ด _์์_ ํด๋น ๋ฏธ๋ค์จ์ด๋ฅผ ์ถ๊ฐํด์ผ ํฉ๋๋ค.
๊ด์ฐฎ์์ง ์๋์ง ์ ๋ชจ๋ฅด๊ฒ ์ต๋๋ค. ๊ฒ์ ์์ฒญ์ ์บก์ฒํ๊ธฐ ์ํด ์ฌ๋ฌ ๊ฐ์ง ๋ฐฉ๋ฒ์ ์ฌ์ฉํ๋ ค๊ณ ์๋ํ๊ณ ์์ง๋ง ์ฌ์ ํ ํ ์ ์์ต๋๋ค. ์ฆ, 'get' ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ ์น์ฌ์ดํธ์์ ์์ฒญ์ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ ๋ถ๋ช ํ ๋ฌธ์ ๊ฐ ์์ต๋๋ค. Django๋ฅผ ์ฌ์ฉํ๋ ๋ชฉ์ ์ ๋ชจ๋ฐ์ผ ํด๋ผ์ด์ธํธ์ฉ ์๋ฒ๋ฅผ ๊ฐ๋ฐํ๊ธฐ ์ํ ๊ฒ์ด๋ฉฐ ๋๋ถ๋ถ์ ์์ฒญ ๋ฐฉ์์ 'POST'์ ๋๋ค. ์ฌ๋ฌ๋ถ์ด ์ ๋ฅผ ๋์ธ ์ ์๊ธฐ๋ฅผ ๋ฐ๋๋๋ค. ๊ฐ์ฌํฉ๋๋ค.
๋๋ฅผ ์ํด ์ผํ ๊ฒ์ (Django REST Framework์ ํ์ ๊ฐ๋ฅํ API์ ํจ๊ป) ์ด django-debug-toolbar-history ์ ๋๋ค.
django-debug-toolbar-history ๋ ํ์ ๊ฐ๋ฅํ DRF ๊ฐ๋ฐ์ ํ์ด์ง๋ฅผ ์ฌ์ฉํ์ฌ ์ ์๊ฒ๋ ํจ๊ณผ์ ์ด์์ต๋๋ค.
์ด๋ฅผ ํตํด POST ๋์ ์ฌ์ฉ๋ SQL ์ฟผ๋ฆฌ๋ฅผ ์ถ์ ํ ์ ์์์ต๋๋ค.
ํ์คํ ๋ฆฌ ํจ๋์ด ๊ณต๊ฐ๋จ์ ๋ฐ๋ผ ์ข ๋ฃํฉ๋๋ค. ๋ํ ๋ฐํ๋๋ POST ์์ฒญ ๋ฐ HTML ์๋ต์ ๊ฒฝ์ฐ ๋ฐ๋ท ๋๊ตฌ ๋ชจ์์ด ํ์๋๋ ๊ฒ๋ ํ์ธํ์ต๋๋ค.
์ต์ ๋ฆด๋ฆฌ์ค๋ก ์ ๋ฐ์ดํธํ๊ณ ๊ณ์ํด์ ๋ฒ๊ทธ๊ฐ ๋ฐ์ํ๋ฉด ์๋ก์ด ์ธ๋ถ ์ ๋ณด๋ก ๋ค์ ์ฌ์ญ์์ค. ๊ฐ์ฌ ํด์.
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ
๋ค์์ ์ด ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํ ๋ฏธ๋ค์จ์ด์ ๋๋ค.
Django Debug Toolbar์ ๋ฏธ๋ค์จ์ด _์์_ ํด๋น ๋ฏธ๋ค์จ์ด๋ฅผ ์ถ๊ฐํด์ผ ํฉ๋๋ค.