Search code examples
pythonhtmldjangohrefurl-parameters

How to pass more than one parameter to url in django


HTML page has 2 button to process payment, I need to pass 2 parameters to url for a reason. Below is the method I tried but it's not working. Can someone help me here..??

<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'),
]

Solution

  • This is because your variable is not defined.

    <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>.

    Also there seems you are not checking right 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}))```