gpt4 book ai didi

django - Stripe Integration 与 Django 给出类型错误,指出 __init__() 采用 1 个位置参数,但给出了 2 个

转载 作者:行者123 更新时间:2023-12-03 08:17:48 26 4
gpt4 key购买 nike

您好,我一直在尝试学习如何将 Stripe 预构建结帐页面集成到我的 django 项目中。

stripe 文档是针对 Flask 的,所以我正在尝试阅读或观看 YouTube 教程,了解如何将它们转换为 django,但我并没有真正取得任何进展。教程中的代码与当前的 stripe 文档不同

https://stripe.com/docs/checkout/integration-builder

from django.shortcuts import render, redirect
import stripe
from django.conf import settings
from django.http import JsonResponse
# Create your views here.
from django.views import View
from django.views.generic import TemplateView

stripe.api_key = settings.STRIPE_SECRET_KEY


class ProductLandingPageView(TemplateView):
template_name = "landing.html"


class CreateCheckoutSessionView(View):
def post(self, request, *args, **kwargs):
YOUR_DOMAIN = "http://127.0.0.1:8000"
checkout_session = stripe.checkout.Session.create(
payment_method_types=['card'],
line_items=[
{
# TODO: replace this with the `price` of the product you want to sell
'price': '{{PRICE_ID}}',
'quantity': 1,
},
],
mode='payment',
success_url=YOUR_DOMAIN + "/success",
cancel_url=YOUR_DOMAIN + "/cancel",
)

return redirect(checkout_session.url, code=303)


class Successview(TemplateView):
template_name = "success.html"


class Cancelview(TemplateView):
template_name = "cancel.html"

我可以显示 checkout.html,但是当我单击结账按钮时,出现此错误

TypeError at /create-checkout-session
__init__() takes 1 positional argument but 2 were given
Request Method: POST
Request URL: http://127.0.0.1:8000/create-checkout-session
Django Version: 3.2.5
Exception Type: TypeError
Exception Value:
__init__() takes 1 positional argument but 2 were given
Exception Location: C:\Program Files\Python39\lib\site-packages\django\core\handlers\base.py, line 181, in _get_response
Python Executable: C:\Program Files\Python39\python.exe
Python Version: 3.9.5
Python Path:
['C:\\Users\\TYS\\Desktop\\Web Development\\Django\\stripetest_project',
'C:\\Program Files\\Python39\\python39.zip',
'C:\\Program Files\\Python39\\DLLs',
'C:\\Program Files\\Python39\\lib',
'C:\\Program Files\\Python39',
'C:\\Users\\TYS\\AppData\\Roaming\\Python\\Python39\\site-packages',
'C:\\Program Files\\Python39\\lib\\site-packages',
'C:\\Program Files\\Python39\\lib\\site-packages\\ibapi-9.76.1-py3.9.egg',
'C:\\Program Files\\Python39\\lib\\site-packages\\win32',
'C:\\Program Files\\Python39\\lib\\site-packages\\win32\\lib',
'C:\\Program Files\\Python39\\lib\\site-packages\\Pythonwin']
Server time: Tue, 17 Aug 2021 03:52:08 +0000

我也尝试过此页面上的代码

https://stripe.com/docs/payments/accept-a-payment?platform=web&ui=checkout

from django.shortcuts import render, redirect
import stripe
from django.conf import settings
from django.http import JsonResponse
# Create your views here.
from django.views import View
from django.views.generic import TemplateView

stripe.api_key = settings.STRIPE_SECRET_KEY


class ProductLandingPageView(TemplateView):
template_name = "landing.html"


class CreateCheckoutSessionView(View):
def post(self, request, *args, **kwargs):
YOUR_DOMAIN = "http://127.0.0.1:8000"
checkout_session = stripe.checkout.Session.create(
payment_method_types=['card'],
line_items=[{
'price_data': {
'currency': 'usd',
'product_data': {
'name': 'T-shirt',
},
'unit_amount': 2000,
},
'quantity': 1,
}],
mode='payment',
success_url=YOUR_DOMAIN + "/success",
cancel_url=YOUR_DOMAIN + "/cancel",
)

return redirect(checkout_session.url, code=303)


class Successview(TemplateView):
template_name = "success.html"


class Cancelview(TemplateView):
template_name = "cancel.html"

仍然得到同样的错误

谁能帮我解决这个问题吗?

谢谢

编辑:添加urls.py以及landing.html以供引用

from django.contrib import admin
from django.urls import path
from products.views import CreateCheckoutSessionView, ProductLandingPageView, Cancelview, Successview

urlpatterns = [
path('admin/', admin.site.urls),
path('cancel/', Cancelview.as_view(), name='cancel'),
path('success/', Successview.as_view(), name='success'),
path('create-checkout-session', CreateCheckoutSessionView, name='create-checkout-session'),
path('', ProductLandingPageView.as_view(), name='landing-page')


]

这是登陆.html

<!DOCTYPE html>
<html>
<head>
<title>Buy cool new product</title>
<link rel="stylesheet" href="style.css">
<script src="https://polyfill.io/v3/polyfill.min.js?version=3.52.1&features=fetch"></script>
<script src="https://js.stripe.com/v3/"></script>
</head>
<body>
<section>
<div class="product">
<img
src="https://i.imgur.com/EHyR2nP.png"
alt="The cover of Stubborn Attachments"
/>
<div class="description">
<h3>Stubborn Attachments</h3>
<h5>$20.00</h5>
</div>
</div>
<form action="/create-checkout-session" method="POST">{% csrf_token %}
<button type="submit">Checkout</button>
</form>
</section>
</body>
</html>

最佳答案

看起来这与 path 定义中缺少 as_view() 有关: https://stackoverflow.com/a/53572256/12474862

这似乎不是与 Stripe 相关的错误,而是 Django 配置中的某些错误。

关于django - Stripe Integration 与 Django 给出类型错误,指出 __init__() 采用 1 个位置参数,但给出了 2 个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68811545/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com