Django returns 403 error when sending a POST request

后端 未结 7 2091
名媛妹妹
名媛妹妹 2021-01-31 08:27

when I\'m using following Python code to send a POST request to my Django website I\'m getting 403: Forbidden error.

url = \'http://www.sub.domain.com/\'
values          


        
7条回答
  •  北恋
    北恋 (楼主)
    2021-01-31 08:32

    Look here https://docs.djangoproject.com/en/dev/ref/csrf/#how-to-use-it.

    Try marking your view with @csrf_exempt. That way, Django's CSRF middleware will ignore CSRF protection. You'll also need to use from django.views.decorators.csrf import csrf_exempt. See: https://docs.djangoproject.com/en/dev/ref/csrf/#utilities

    Please be advised that by disabling CSRF protection on your view, you are opening a gate for CSRF attacks.

    If security is vital to you then consider using @csrf_exempt followed by @requires_csrf_token (see: https://docs.djangoproject.com/en/dev/ref/csrf/#unprotected-view-needs-the-csrf-token). Then, in your script pass this token and that's it.

提交回复
热议问题