Django returns 403 error when sending a POST request

后端 未结 7 2240
名媛妹妹
名媛妹妹 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:27

    Or you can allow the permission to make this post request.

    Note: Should be used in the cases where you don't need to authenticate the users for posting anything on our server, say, when a new user registers for the first time.

    from rest_framework.permissions import AllowAny
    
    class CreateUser(APIView):
        permission_classes = (AllowAny,)
        def post(self, request, format=None):
            return(Response("hi"))
    

    Further Note that, If you want to make that post request form a different domain (in case when the front of the application is in React or angular and the backend is in Django), make sure the add following in the settings file:

    1. Update the INSTALLED_APPS to use 'coreHeaders' :

      INSTALLED_APPS = [
      'corsheaders',
      ]

    2. White list your front end domain by adding following to settings file again:

      CORS_ORIGIN_WHITELIST = ( 'localhost:8080', )

提交回复
热议问题