LoginSignup
2
1

More than 1 year has passed since last update.

[Django] request オブジェクトの中身が見たい

Last updated at Posted at 2022-07-22

requestの中身を見たい

Djangoで開発中に request の中身を見たいと思ったことはありませんか? 僕はあります。
django-debug-toolbar 使えと言われたらそれまでなんですが、コードベースで見れるならその方がいいですよね。

方法

views.py
def index(request)
    print(request.__dict__)
    # または
    print(vars(request))

出力

bash
{
    '_request': <ASGIRequest: PATCH'/api/notifications/6/'>,
    'parsers': [<rest_framework.parsers.JSONParser object at 0x10ef9e620>,
    <rest_framework.parsers.FormParser object at 0x10ef9e320>,
    <rest_framework.parsers.MultiPartParser object at 0x10ef9de70>],
    'authenticators': [<rest_framework.authentication.TokenAuthentication object at 0x10ef9ea10>,
    <rest_framework.authentication.SessionAuthentication object at 0x10ef9ca90>],
    'negotiator': <rest_framework.negotiation.DefaultContentNegotiation object at 0x10ef9c400>,
    'parser_context': {'view': <admin_api.views.NotificationViewSet object at 0x10ef9d8d0>,
    'args': (),
    'kwargs': {'pk': '6'},
    'request': <rest_framework.request.Request: PATCH '/api/notifications/6/'>,
    'encoding': 'utf-8'},
    '_data': {'send_time': '2022-07-26T17:22:00.000Z',
    'contents': '2123123',
    'country': [2]},
    '_files': <MultiValueDict: {}>,
    '_full_data': {'send_time': '2022-07-26T17:22:00.000Z',
    'contents': '2123123',
    'country': [2]},
    '_content_type': <class 'rest_framework.request.Empty'>,
    '_stream': <ASGIRequest: PATCH '/api/notifications/6/'>,
    'accepted_renderer': <game_api.renderers.UTF8JSONRenderer object at 0x10ef9d840>,
    'accepted_media_type': 'application/json',
    'version': None,
    'versioning_scheme': None,
    '_authenticator': <rest_framework.authentication.TokenAuthentication object at 0x10ef9ea10>,
    '_user': <User: foo@example.com>,
    '_auth': <Token: 4943996d8d43c13b6ccb12ac6da0e148a5>
}

それぞれの意味は 公式ドキュメント Request and response objects を参考にしてください。

2
1
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1