【问题标题】:Django redirect in an if statement not workingDjango在if语句中重定向不起作用
【发布时间】:2019-11-13 09:52:19
【问题描述】:

我在 Django 中遇到了一个我无法解决的错误。在接收到我的端点的帖子后,我试图比较结果代码是否等于 0,如果它等于 0,然后我重定向到相关视图。

views.py

def home(request):
if request.method == 'POST':
    messages.add_message(request, messages.SUCCESS, 'Transaction Intited Successfully. Enter PIN on your phone')

    phone_no = request.POST['client_phone'][1:]

    base_url = settings.BASE_URL
    lipa_time = datetime.now().strftime('%Y%m%d%H%M%S')
    Business_short_code = settings.BUSINESS_SHORTCODE
    passkey = settings.PASSKEY
    data_to_encode = Business_short_code + passkey + lipa_time
    online_password = base64.b64encode(data_to_encode.encode())
    decode_password = online_password.decode('utf-8')

    lipa = Lipa()
    access_token = lipa.get_token()
    print(access_token)
    api_url = "https://sandbox.safaricom.co.ke/mpesa/stkpush/v1/processrequest"
    headers = {"Authorization": "Bearer %s" % access_token}
    request = {
        "BusinessShortCode": Business_short_code,
        "Password": decode_password,
        "Timestamp": lipa_time,
        "TransactionType": "CustomerPayBillOnline",
        "Amount": "1",
        "PartyA": "254"+phone_no, 
        "PartyB": Business_short_code,
        "PhoneNumber": "254"+phone_no,  
        "CallBackURL": base_url+"/confirmation",
        "AccountReference": "Jaymoh",
        "TransactionDesc": "Channel join payment"
    }
    response = requests.post(api_url, json=request, headers=headers)
    pprint.pprint(response.json())

    rendered = render_to_string('home.html', {})
    response = HttpResponse(rendered)

    return response

return render(request, 'home.html', {})

发布数据被发送到下面的视图:

def confirmation(request):
print(request.body)
if request.method == 'POST':
    response = json.loads(request.body)
    pprint.pprint(response)
    transaction_response = response['Body']['stkCallback']

    save_transaction = Transaction( 
        MerchantRequestID = transaction_response['MerchantRequestID'],
        CheckoutRequestID = transaction_response['CheckoutRequestID'],
        ResultCode = transaction_response['ResultCode'],
        ResultDesc = transaction_response['ResultDesc']
    )

    save_transaction.save()

    transaction_result = transaction_response['ResultCode']
    print("ResultCode = %s" % transaction_result)
    print(type(transaction_result))

    if transaction_result == 0:
        print('Transaction successful')
        return redirect("successfultransaction")

    else:
        print("Incomplete Transaction")
        return redirect('incompletetransaction') 

return render(request, 'home.html', {})

错误现在出现在这个视图中,if 语句执行并打印,但重定向从未发生。

{"Body":{"stkCallback":{"MerchantRequestID":"8995-63446-1","CheckoutRequestID":"ws_CO_131120191221087793","ResultCode":1,"ResultDesc":"The 交易余额不足"}}}'

{'Body': {'stkCallback': {'CheckoutRequestID': 'ws_CO_131120191221087793', 'MerchantRequestID': '8995-63446-1', “结果代码”:1, 'ResultDesc': '余额不足 ' '交易'}}} {'CheckoutRequestID':'ws_CO_131120191221087793','CustomerMessage': '成功。已接受处理请求', 'MerchantRequestID': '8995-63446-1','ResponseCode':'0','ResponseDescription': '成功。已接受处理请求'}

[2019 年 11 月 13 日 12:21:17]“POST/HTTP/1.1”200 4081

结果代码 = 1

交易不完整

[2019 年 11 月 13 日 12:21:17]“POST /确认 HTTP/1.1”302 0


在 home 函数中发帖之前,我返回了一个HttpResponse()。我认为不会影响接收 POST 的确认回调函数中的重定向。

【问题讨论】:

  • 显示你的 urls.py

标签: python django redirect


【解决方案1】:

不,发生了。

看:[13/Nov/2019 12:21:17] "POST /confirmation HTTP/1.1" 302 0

您收到了带有 302 代码的响应,即重定向。

【讨论】:

  • 但我认为当代码执行到 else 部分时不会发生重定向,因为执行继续进行而不返回任何内容。显示的页面是在回调之前在 home 函数上使用 HttpResponse 呈现的页面
猜你喜欢
  • 1970-01-01
  • 2022-06-22
  • 2020-08-09
  • 2017-05-22
  • 1970-01-01
  • 2018-08-28
  • 2012-11-30
  • 2023-02-06
  • 1970-01-01
相关资源
最近更新 更多