【问题标题】:django rest cant get session varible in get requestdjango rest 无法在获取请求中获取会话变量
【发布时间】:2018-03-07 14:18:31
【问题描述】:

我有以下关于用户付款进度的类 当请求是POST 时,我正在像这样request.session['amount'] = amount 在会话中设置金额 当请求是GET 时,我应该得到会话,但会话是空的,if 语句if 'amount' in request.session: 总是错误的。我该如何解决这个问题??

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)

【问题讨论】:

  • rest-framework 中使用的请求与普通的 django 请求不同,但是只要您使用 SessionAuth 而不是任何 TokenAuths,会话应该是可用的。
  • 而且您的代码可能不应该信任客户端收取多少费用,请在后端进行计算。

标签: python django python-3.x django-rest-framework


【解决方案1】:

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

【讨论】:

  • 我使用了 JSONWebTokenAuthentication
猜你喜欢
  • 2018-10-31
  • 2018-01-07
  • 1970-01-01
  • 1970-01-01
  • 2011-12-09
  • 1970-01-01
  • 2019-03-04
  • 2018-05-30
  • 1970-01-01
相关资源
最近更新 更多