django rest无法在get请求中获取会话变量

时间:2021-01-12 18:21:36

I have the following class for user's payment progress when the request is POST, I am setting amount in the session like this request.session['amount'] = amount and when the request is GET i should get the session but session is empety and the if statement if 'amount' in request.session: is always false. How can I fix this??

当请求是POST时,我有以下类用于用户的支付进度,我在会话中设置金额,如此request.session ['amount'] =金额,当请求是GET我应该得到会话但会话是安全的, if语句如果request.session中的'amount',则始终为false。我怎样才能解决这个问题??

class PaidApiView(APIView):
    MMERCHANT_ID = '??????????'  # Required
    ZARINPAL_WEBSERVICE = 'https://www.zarinpal.com/pg/services/WebGate/wsdl'  # Required
    amount = None  # Required
    description = None  # Required
    products = None
    email = 'user@userurl.ir'  # Optional
    mobile = '09123456789'  # Optional
    CallbackURL = 'http://127.0.0.1:8000/store/paid/'

    def post(self, request, *args, **kwargs):

        amount = request.data['amount']
        products = request.data['products']
        productstitle = request.data['productstitle']
        productsTitlestr = ''.join(productstitle)

        self.amount = amount
        self.products = products
        self.description = productsTitlestr

        request.session['amount'] = amount


        client = Client(self.ZARINPAL_WEBSERVICE)
        result = client.service.PaymentRequest(self.MMERCHANT_ID,
                                               self.amount,
                                               self.description,
                                               self.email,
                                               self.mobile,
                                               self.CallbackURL)
        if result.Status == 100:
            return Response('https://www.zarinpal.com/pg/StartPay/' + result.Authority)
        else:
            return Response('Error')

    def get(self, request):

        if 'amount' in request.session:
            self.amount = request.session['amount']

            client = Client(self.ZARINPAL_WEBSERVICE)
            if request.GET.get('Status') == 'OK':
                result = client.service.PaymentVerification(self.MMERCHANT_ID,
                                                            request.GET['Authority'],
                                                            self.amount)
                if result.Status == 100:
                    del request.session['amount']
                    return Response('Transaction success. RefID: ' + str(result.RefID))
                elif result.Status == 101:
                    del request.session['amount']
                    return Response('Transaction submitted : ' + str(result.Status))
                else:
                    del request.session['amount']
                    return Response('Transaction failed. Status: ' + str(result.Status))
            else:
                del request.session['amount']
                return Response('Transaction failed or canceled by user')

        else:
            return Response(status=status.HTTP_400_BAD_REQUEST)

1 个解决方案

#1


0  

If you have activated the SessionAuthentication class you will have the session variable. But probably you are using TokenAuthentication, so your best option is to save the data in a table and load it on every request.

如果已激活SessionAuthentication类,则将具有会话变量。但是您可能正在使用TokenAuthentication,因此最好的选择是将数据保存在表中并在每个请求中加载它。

#1


0  

If you have activated the SessionAuthentication class you will have the session variable. But probably you are using TokenAuthentication, so your best option is to save the data in a table and load it on every request.

如果已激活SessionAuthentication类,则将具有会话变量。但是您可能正在使用TokenAuthentication,因此最好的选择是将数据保存在表中并在每个请求中加载它。