【问题标题】:Redirecting a user to checkout page in Django将用户重定向到 Django 中的结帐页面
【发布时间】:2021-11-12 08:22:31
【问题描述】:

我想将用户重定向到支付网关自动生成的结帐页面。每当用户提交请求时,支付网关都会以以下格式返回响应。我想要做的是将用户重定向到结帐页面进行付款。

 "status": true,
  "message": "Authorization URL created",
  "data": {
    "authorization_url": "https://checkout.paystack.com/eddno1f6rf411p0",
    "access_code": "eddno1f6rf411p0",
    "reference": "bpozkels2v"

def paystack(request):
    url = 'https://api.paystack.co/transaction/initialize'
    transaction_id = random.randint(100000000000, 999999999999)
    data = {
            "key": "PUBLIC_KEY",
            "ref": transaction_id,
            "amount": "000000000100",
            "callback": f"http://127.0.0.1:8000",
            "email": "email@me.com",
        }
    headers =  {
        'Content-Type': 'application/json',
        'Authorization': 'Bearer SECRET_KEY',
        'Cache-Control': 'no-cache'
    }
    res = requests.post(url, data=json.dumps(data), headers=headers)
    response_data = res.json()
    print(response_data["authorization_url"])
    redirect(response_data["authorization_url"])

我得到的错误是

 KeyError at /paystack
'authorization_url'

【问题讨论】:

  • 不应该是response_data["data"]["authorization_url"]吗?

标签: python django django-views python-requests django-forms


【解决方案1】:

鉴于响应与问题文本中的一样,您首先应该访问"data" 键的子字典,因此:

response_data<strong>['data']</strong>['authorization_url']

【讨论】:

  • 我在重定向The view vote.views.paystack didn't return an HttpResponse object. It returned None instead 中得到了一个新错误。无论如何围绕它? @Willem Van Onsem
  • @spiritman:你需要return redirect(..)只是调用redirect函数。
  • 成功了!像魔术一样!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-27
  • 2022-09-29
  • 2020-03-14
  • 1970-01-01
  • 1970-01-01
  • 2016-09-29
相关资源
最近更新 更多