【问题标题】:Django and Stripe, Module 'stripe' has no 'Customer' memberDjango 和 Stripe,模块 'stripe' 没有 'Customer' 成员
【发布时间】:2021-01-05 02:43:45
【问题描述】:

所以我正在尝试使用 django 在条带中创建一个客户,根据条带中的文档,代码 stripe.Customer.create 应该可以工作,但它只是出现了这个错误,有人能告诉我为什么会这样吗?

views.py

from django.shortcuts import render, redirect
from django.urls import reverse
from django.http import JsonResponse

import stripe
# Create your views here.
stripe.api_key = "xxxx"

def index(request):
    return render(request, 'sales/index.html')

def charge(request):
    amount = 5
    if request.method == 'POST':
        print('Data', request.POST)

        stripe.Customer.create(
            email=request.POST['email']
        )

    return redirect(reverse('success', args=[amount]))

def successMsg(request, args):
    amount = args
    return render(request, 'sales/success.html', {'amount':amount})


追溯

Traceback (most recent call last):
  File "C:\Users\snin2\anaconda3\envs\MyDjangoEnv\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
    response = get_response(request)
  File "C:\Users\snin2\anaconda3\envs\MyDjangoEnv\lib\site-packages\django\core\handlers\base.py", line 179, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\snin2\Desktop\django braintree\stripe\sales\views.py", line 17, in charge
    stripe.Customer.create(
AttributeError: module 'stripe' has no attribute 'Customer'
[18/Sep/2020 08:36:39] "POST /charge/ HTTP/1.1" 500 66773


【问题讨论】:

  • 可以粘贴回溯吗?
  • 您遇到错误了吗?最可能的罪魁祸首是stripe-python库没有正确安装。
  • 嘿,我上传了回溯,有什么解决办法吗?
  • 您的项目中是否有任何名为stripe.py 的文件?能否将完整的目录结构添加到 OP 中?
  • 不,我没有 stripe.py

标签: python django frameworks stripe-payments payment


【解决方案1】:

导致此错误的唯一原因是您的项目目录中可能有stripe.py 文件或模块。

而且,是的,您在 django braintree 目录下有一个名为 stripe

\django braintree<b>\stripe\</b>sales\views.py

我们如何验证这一点?

您可以通过检查模块的.__file__ 属性来检查模块的位置

import stripe
print(stripe.__file__)
            ^^^^^^^^^^^

参考文献

  1. Python Stripe: 'module' object has no attribute 'Charge' -- (StackOverlofw)
  2. AttributeError: 'module' object has no attribute 'Charge' -- (GitHub)

【讨论】:

  • 确实,OP 有一个stripe 模块:C:\Users\snin2\Desktop\django braintree\ stripe \sales\views.py.
【解决方案2】:

您已将您的项目/应用程序命名为 stripe,这与您安装的 stripe 包发生冲突

将您的项目/应用名称更改为其他名称,然后重试

注意:避免使用包名作为应用名或项目名,这会在引用过程中产生问题

【讨论】:

    猜你喜欢
    • 2021-06-27
    • 2016-02-18
    • 1970-01-01
    • 2014-01-16
    • 2021-01-03
    • 2021-02-04
    • 2016-05-01
    • 2021-01-04
    • 2019-02-07
    相关资源
    最近更新 更多