if语句中的Django重定向无效

2024-10-02 08:17:49 发布

您现在位置:Python中文网/ 问答频道 /正文

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

视图.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', {})

Post数据发送到以下视图:

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 balance is insufficient for the transaction"}}}'

{'Body': {'stkCallback': {'CheckoutRequestID': 'ws_CO_131120191221087793', 'MerchantRequestID': '8995-63446-1', 'ResultCode': 1, 'ResultDesc': 'The balance is insufficient for the ' 'transaction'}}} {'CheckoutRequestID': 'ws_CO_131120191221087793', 'CustomerMessage': 'Success. Request accepted for processing', 'MerchantRequestID': '8995-63446-1', 'ResponseCode': '0', 'ResponseDescription': 'Success. Request accepted for processing'}

[13/Nov/2019 12:21:17] "POST / HTTP/1.1" 200 4081

ResultCode = 1

Incomplete Transaction

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


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


Tags: 视图homereturnifresponserequestphonepost

热门问题