Python Django和Stripe多值DictKeyError at/checkout/"';条纹肯'&引用;
那是我的结账单 请帮忙,我会很感激的。 我正在制作一个网站,最终收取付款,但当我运行它时,我发现这个错误,我认为它来自views.py,但我不确定是否想从一些djangonites或pythoners那里得到一些反馈,谢谢Django在本例中,当请求的键Python Django和Stripe多值DictKeyError at/checkout/"';条纹肯'&引用;,python,django,stripe-payments,Python,Django,Stripe Payments,那是我的结账单 请帮忙,我会很感激的。 我正在制作一个网站,最终收取付款,但当我运行它时,我发现这个错误,我认为它来自views.py,但我不确定是否想从一些djangonites或pythoners那里得到一些反馈,谢谢Django在本例中,当请求的键stripeToken在请求中不存在时,会引发。您可能需要检查您的POST表单数据以满足您的请求 views.py from django.conf import settings from django.shortcut
stripeToken
在请求中不存在时,会引发。您可能需要检查您的POST
表单数据以满足您的请求
views.py
from django.conf import settings
from django.shortcuts import render
from django.contrib.auth.decorators import login_required
import stripe
stripe.api_key = settings.STRIPE_SECRET_KEY
# Create your views here.
@login_required
def checkout(request):
publishKey = settings.STRIPE_PUBLISHABLE_KEY
if request.method == 'POST':
token = request.POST['stripeToken']
try:
stripe.Charge.create(
amount=1000,
currency="usd",
card=token,
description="Charge for test@example.com"
)
except stripe.CardError:
# The card has been declined
pass
# Create the charge on Stripe's servers - this will charge the user's card
context = {'publishKey': publishKey
}
template = 'checkout.html'
return render(request, template, context)
When i run my views.py gives me this error - MultiValueDictKeyError at /checkout/
"'stripeToken'"
here is my checkout.py
MultiValueDictKeyError at /checkout/
"'stripeToken'"
此类[多值dict
]的存在是为了解决令人恼火的问题
由cgi.parse_qs引发,它返回每个键的列表,甚至
尽管大多数Web表单都提交单个名称-值对
cgi.parse_qs
:解析作为字符串参数给出的查询字符串(数据类型为application/x-www-form-urlencoded)。数据返回为
一本字典。字典键是唯一的查询变量名
这些值是每个名称的值列表
现在使用一个不存在的密钥:
>>> from django.utils.datastructures import MultiValueDict as MVD
>>> data = MVD({'name': ['John'], 'email': ['j@yahoo.com']})
>>> data['name']
'John'
>>> data['email']
'j@yahoo.com'
你也犯了类似的错误request.POST
是一个多值dict
,它包含来自POST数据的键/值对。错误是说,“stripeToken”
不是多值dict中的键,这意味着在POST数据中没有具有键“stripeToken”
的键/值对
如果数据来自html表单,则表示没有将其name
属性设置为“stripeToken”的表单输入元素,例如
我也有同样的问题,问题的原因是checkout.html。我在用这个
<input type="text" name="stripeToken">
而不是
Stripe.setPublishableKey({{ publishkey }});
更正它为我解决了错误刷新您的可发布和机密api密钥,删除所有测试数据,然后尝试在您的终端上应用并打印令牌,以查看其是否实际通过:
Stripe.setPublishableKey('{{ publishkey }}');
Stripe.setPublishableKey({{ publishkey }});
Stripe.setPublishableKey('{{ publishkey }}');
if 'stripeToken' in request.POST:
print(request.POST['stripeToken'])