- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
您好,我一直在尝试学习如何将 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/
为什么正是是 A.__init__() B.__init__() D.__init__() 由以下代码打印?特别是: 为什么是C.__init__() 未打印? 为什么是C.__init__()如果我
目前我有这样的事情: @dataclass(frozen=True) class MyClass: a: str b: str c: str d: Dict[str, str] ...
我正在尝试从父类继承属性: class Human: def __init__(self,name,date_of_birth,gender,nationality): self.name =
如何扩展基类的 __init__,添加更多要解析的参数,而不需要 super().__init__(foo, bar) 在每个派生类中? class Ipsum: """ A base ips
这是我试图解决的一个非常简单的例子: class Test(object): some_dict = {Test: True} 问题是我无法在 Test 仍在定义时引用它 通常,我会这样做:
我在 Objective-C 中使用过这个结构: - (void)init { if (self = [super init]) { // init class }
我有一个类层次结构,其中 class Base 中的 __init__ 执行一些预初始化,然后调用方法 calculate。 calculate 方法在 class Base 中定义,但预计会在派生类
这是我在多种语言中都怀念的一个特性,想知道是否有人知道如何在 Python 中完成它。 我的想法是我有一个基类: class Base(object): def __init__(self):
我正在对 threading.Thread 类进行子类化,它目前看起来像这样: class MyThread(threading.Thread): def __init__(self:
我正在用 cython 写一些代码,我有一些 "Packages “within” modules" . — 这实际上是对我在那里的问题的跟进,结构应该是一样的。问题是这是 cython,所以我处理的
class AppendiveDict(c.OrderedDict): def __init__(self,func,*args): c.OrderedDict.__init_
看完this回答,我明白 __init__ 之外的变量由类的所有实例和 __init__ 内的变量共享每个实例都是唯一的。 我想使用所有实例共享的变量,随机给我的类实例一个唯一的参数。这是我尝试过的较
在下面的代码中: import tkinter as tk class CardShuffling(tk.Tk): background_colour = '#D3D3D3'
我正在覆盖类的 __new__() 方法以返回具有特定 __init__() 集的类实例。 Python 似乎调用类提供的 __init__() 方法而不是特定于实例的方法,尽管 Python 文档在
从内置类型和其他类派生时,内置类型的构造函数似乎没有调用父类(super class)构造函数。这会导致 __init__ 方法不会被 MRO 中内置函数之后的类型调用。 例子: class A:
答: super( BasicElement, self ).__init__() 乙: super( BasicElement, self ).__init__( self ) A 和 B 有什么区
class A(object): def __init__(self): print('A.__init__()') class D(A): def __init__(
到目前为止我已经成功地做了什么: 我创建了一个 elem 类来表示 html 元素(div、html、span、body 等)。 我可以像这样派生这个类来为每个元素创建子类: class elem:
我一直在努力理解 super() 在多重继承的上下文中的行为。我很困惑为什么在 test2.py 的父类中调用 super() 会导致为父类调用 __init__()? test1.py #!/usr
为什么我在 Python 代码中看不到以下内容? class A: def __init__(self, ...): # something important class B
我是一名优秀的程序员,十分优秀!