gpt4 book ai didi

python - QueryDict 不包含隐藏的表单字段,给出 MultiValueDictKeyError

转载 作者:太空宇宙 更新时间:2023-11-04 04:09:22 24 4
gpt4 key购买 nike

我正在尝试将 Razorpay 支付网关与 Django 集成。如何获取 Razorpay 在我的 View 中自动发送的隐藏字段?

checkout/payment_details.html

<form action="{% url 'checkout:preview' %}" method="POST">
<!-- Note that the amount is in its subunit value = 599 -->
<script
src="https://checkout.razorpay.com/v1/checkout.js"
data-key="rzp_test_YYUUnrrBMW42iL"
data-amount="{{ order_total_incl_tax_cents }}"
data-currency="INR"
data-buttontext="Pay with Razorpay"
data-name="{{ shop_name }}"
data-description="{{ basket.num_items }} items ({{ order_total.incl_tax|currency }})"
data-image="https://your-awesome-site.com/your_logo.jpg"
data-prefill.name="Gaurav Kumar"
data-prefill.email="test@test.com"
data-theme.color="#F37254"
></script>
<input type="hidden" value="{{ razorpay_token_form }}" name="razorpay_payment_id">
</form>

checkout/views.py

class PaymentDetailsView(CorePaymentDetailsView):

@method_decorator(csrf_exempt)
def dispatch(self, request, *args, **kwargs):
return super(PaymentDetailsView, self).dispatch(request, *args, **kwargs)

def get_context_data(self, **kwargs):
ctx = super(PaymentDetailsView, self).get_context_data(**kwargs)
if self.preview:
ctx['razorpay_token_form'] = forms.RazorpayTokenForm(self.request.POST)
ctx['order_total_incl_tax_cents'] = (
ctx['order_total'].incl_tax * 100
).to_integral_value()
else:
ctx['order_total_incl_tax_cents'] = (
ctx['order_total'].incl_tax * 100
).to_integral_value()
ctx['razorpay_publishable_key'] = settings.RAZORPAY_PUBLISHABLE_KEY
return ctx

def handle_payment(self, order_number, total, **kwargs):
print("requst in handle payment")
print(self.request.GET)
razorpay_ref = Facade().charge(
order_number,
total,
card=self.request.POST['razorpay_payment_id', False],
description=self.payment_description(order_number, total, **kwargs),
metadata=self.payment_metadata(order_number, total, **kwargs))

source_type, __ = SourceType.objects.get_or_create(name=PAYMENT_METHOD_RAZORPAY)
source = Source(
source_type=source_type,
currency=settings.RAZORPAY_CURRENCY,
amount_allocated=total.incl_tax,
amount_debited=total.incl_tax,
reference=razorpay_ref)
self.add_payment_source(source)

self.add_payment_event(PAYMENT_EVENT_PURCHASE, total.incl_tax)

堆栈跟踪

Traceback (most recent call last):
File "C:\Users\kingmaker\AppData\Local\Programs\Python\Python37-32\lib\site-packages\oscar\apps\checkout\views.py", line 580, in submit
self.handle_payment(order_number, order_total, **payment_kwargs)
File "C:\Users\kingmaker\workspace\blackweb_release_2.0\blackweb_services\checkout\views.py", line 44, in handle_payment
card=self.request.POST['razorpay_payment_id', False],
File "C:\Users\kingmaker\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\utils\datastructures.py", line 85, in __getitem__
raise MultiValueDictKeyError(repr(key))
django.utils.datastructures.MultiValueDictKeyError: "('razorpay_payment_id', False)"

打印 print(self.request.POST) 给出

 <QueryDict: {'csrfmiddlewaretoken': ['jmeHeDEeNhaLM591S4113k1nNiXPLlPOT7eO47pqFyweLRw5al5ldrafTQmM9JPR'], 'action': ['place_order']}>

如何将 payment_id 放入 querydict 中?

最佳答案

表达式:

self.request.POST['razorpay_payment_id', False]

在这里没有多大意义。这意味着您查询 2 元组 ('razorpay_payment_id', False),但如果我理解正确,您只想使用 False 作为“后备”值(value)。

self.request.POST<b>.get(</b>'razorpay_payment_id', False<b>)</b>

因此我们在这里使用 QueryDict.get(..) [Django-doc]带有两个参数的函数:要搜索的键(此处为 'razorpay_payment_id'),以及作为第二个参数的可选默认值,以防键丢失(此处为 False >).

请注意,在您的模板中,您在隐藏字段中呈现了一个表单。您应该渲染一个值,例如:

{{ razorpay_token_form }}
<input type="hidden" custom="Hidden Element" name="hidden">

关于python - QueryDict 不包含隐藏的表单字段,给出 MultiValueDictKeyError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56619691/

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