如何在django中向url传递多个参数?

问题描述 投票:0回答:1

HTML页面有2个按钮来处理付款,我需要给url传递2个参数,原因是什么?下面是我试过的方法,但是没有用。谁能帮帮我...?

<a href="{% url 'process-payment' order.id button.id %}"   id = "CashOnDelivery" class="btn btn-warning"> Cash on Delivery</a> 
<a href="{% url 'process-payment' order.id button.id %}"  id = "Card" class="btn btn-warning">Pay through Card</a>

views.py

def process_payment(request, order_id, button_id):
    if id == CashOnDelivery
     # directly take order
    return redirect (reverse('update-records', kwargs={'order_id': order_id}))

    else 
     # process the card payment then update transaction 
    return redirect (reverse('update-records', kwargs={'order_id': order_id}))

urls.py

urlpatterns=[
 path('payment/<order_id>/<button_id>',views.process_payment, name='process-payment'),
]
python html django href url-parameters
1个回答
0
投票

这是因为你的变量没有定义。

<a href="{% url 'process-payment' order.id 'CashOnDelivery' %}" id = "CashOnDelivery" class="btn btn-warning"> Cash on Delivery</a>.

<a href="{% url 'process-payment' order.id 'Card' %}" id = "Card" class="btn btn-warning">Pay through Card</a>.

另外,你似乎没有检查正确的ID。

def process_payment(request, order_id, button_id):
    if button_id == CashOnDelivery
     # directly take order
    return redirect (reverse('update-records', kwargs={'order_id': order_id}))

    else 
     # process the card payment then update transaction 
    return redirect (reverse('update-records', kwargs={'order_id': order_id}))```
© www.soinside.com 2019 - 2024. All rights reserved.