【问题标题】:why getting 'WSGIRequest' object has no attribute 'data' error?为什么获取“WSGIRequest”对象没有属性“数据”错误?
【发布时间】:2022-01-16 09:40:13
【问题描述】:

我正在尝试在 react Js 和 Django 中通过条纹使用卡支付。我正在关注https://betterprogramming.pub/how-to-integrate-django-react-app-with-stripe-payments-95709b3f23e5这个教程。

前端

const handleSubmit = async (event) => {
    event.preventDefault();
    const card = elements.getElement(CardElement);
    const {paymentMethod, error} = await stripe.createPaymentMethod({
      type: 'card',
      card: card
  });
  ApiService.saveStripeInfo({
    email, payment_method_id: paymentMethod.id})
  .then(response => {
    console.log(response.data);
  }).catch(error => {
    console.log(error)
  })
}
export const api = axios.create({
  baseURL: API_URL,
  headers: {
    "Content-type": "application/json"
  }
});
export default class ApiService{
  static saveStripeInfo(data={}){
    return api.post(`${API_URL}/payments/save-stripe-info/`, data)
  }
}

服务器

@api_view(['POST'])
def test_payment(request):
    test_payment_intent = stripe.PaymentIntent.create(
    amount=1000, currency='pln', 
    payment_method_types=['card'],
    receipt_email='test@example.com')
    return Response(status=status.HTTP_200_OK, data=test_payment_intent)
        
def save_stripe_info(request):
    print('this => ',request.data)
    data = request.data
    email = data['email']
    payment_method_id = data['payment_method_id']
    
    # creating customer
    customer = stripe.Customer.create(
      email=email, payment_method=payment_method_id)
     
    return Response(status=status.HTTP_200_OK, 
      data={
        'message': 'Success', 
        'data': {'customer_id': customer.id} 
      }  
    )       

但是每当我点击提交按钮时,它都会给我以下错误

AttributeError: 'WSGIRequest' 对象没有属性 'data' [12/Dec/2021 21:55:57]“POST /payments/save-stripe-in​​fo/HTTP/1.1”500 71355

完整代码请访问https://betterprogramming.pub/how-to-integrate-django-react-app-with-stripe-payments-95709b3f23e5

【问题讨论】:

    标签: reactjs django stripe-payments


    【解决方案1】:

    根据docs 没有WSGIRequest 的数据成员。您将需要参考 body 属性。

    【讨论】:

    • 我无法理解,请多描述一点@LajosArpad
    • @Ruma 如果你有一个对象,我们称它为foo,然后引用foo.bar 将产生如果foo 没有名为bar 的成员时你遇到的错误。由于您的 request 对象属于 WSGIRequest 类型,因此您需要引用该类型的数据成员,这在我的答案中已链接到。因为在仔细查看文档后,您会得出结论,对于此类对象没有名为 data 的成员,因此很正常,甚至更预期您会收到您在问题中提到的错误。因此,您将需要寻找替代方案。既然那种物体
    • @Ruma 有一个名为body 的成员,您需要引用它,所以将data = request.data 更改为data = request.body,看看body 包含什么。我不是 Python 程序员,所以我在为您编写示例代码时犹豫不决,因为它可能包含语法错误,但您肯定需要在我提到的行中使用 body 而不是 data
    【解决方案2】:

    数据是 rest_framework 的一部分。您需要使用 api_view 来装饰视图。

    只需在 def save_stripe_info(request) 之前添加“@api_view(['POST'])”。 应该是:

    @api_view(['POST'])
    def save_stripe_info(request):
        print('this => ',request.data)
        data = request.data
        email = data['email']
        payment_method_id = data['payment_method_id']
        
        # creating customer
        customer = stripe.Customer.create(
          email=email, payment_method=payment_method_id)
         
        return Response(status=status.HTTP_200_OK, 
          data={
            'message': 'Success', 
            'data': {'customer_id': customer.id} 
          }  
        )  
    

    或者只是在 json 中加载正文。

    data = json.loads(request.body)
    

    【讨论】:

      猜你喜欢
      • 2020-11-22
      • 2015-02-01
      • 1970-01-01
      • 2016-07-30
      • 1970-01-01
      • 2017-02-02
      • 2015-04-15
      • 2013-05-20
      • 2016-09-22
      相关资源
      最近更新 更多