如何在 Django 中将多个参数传递给 url
How to pass more than one parameter to url in django
HTML 页面有 2 个按钮来处理付款,我需要将 2 个参数传递给 url 是有原因的。以下是我尝试过但不起作用的方法。有人可以帮我吗..?
<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'),
]
这是因为你的变量没有定义。
<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}))```
HTML 页面有 2 个按钮来处理付款,我需要将 2 个参数传递给 url 是有原因的。以下是我尝试过但不起作用的方法。有人可以帮我吗..?
<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'),
]
这是因为你的变量没有定义。
<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}))```