【问题标题】:How to pass more than one parameter to url in django如何在django中将多个参数传递给url
【发布时间】:2020-09-06 14:13:28
【问题描述】:

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

【问题讨论】:

  • 您能否至少发布您收到的错误消息?
  • @errorinpersona 错误消息:找不到带有参数“(88,”)的“流程支付”的反向。尝试了 1 种模式:['cart/payment/(?P[^/]+)/(?P[^/]+)$'] 它正在使用 order_id 但是不是 button_id。
  • 不,button.id 只是空的。你应该检查一下。
  • @errorinpersona 这正是我的问题......如何?
  • 最简单的方法是 prolly: {{ button.id }} 在模板中的某处检查它是否打印出一些东西 ;)

标签: python html django href url-parameters


【解决方案1】:

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

&lt;a href="{% url 'process-payment' order.id 'CashOnDelivery' %}" id = "CashOnDelivery" class="btn btn-warning"&gt; Cash on Delivery&lt;/a&gt;.

&lt;a href="{% url 'process-payment' order.id 'Card' %}" id = "Card" class="btn btn-warning"&gt;Pay through Card&lt;/a&gt;.

另外,您似乎没有检查正确的 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}))```

【讨论】:

  • 为什么? button_id 应该是“CashOnDelivery”或“Card”,对吗?因为发送到 url 的第二个参数与 url 模式 path('payment/&lt;order_id&gt;/**&lt;button_id&gt;** 匹配,所以我也在视图中使用了相同的 def process_payment(request, order_id, **button_id**):
  • 您是否在任何地方定义了button.id 变量? URL怎么知道button.id是什么意思?
  • 我只是错过了这部分 &lt;a href="{% url 'process-payment' order.id 'CashOnDelivery' %}" 不知道如何将第二个参数传递给 url。现在它工作正常。谢谢你。再改一改if button_id == "CashOnDelivery" --missed 双引号简单错误。
  • button_id in views 我将从 -->urlpattern button_id 到 urlpattern 我将从这部分获得 -->&lt;a href="{% url 'process-payment' order.id 'CashOnDelivery' %}"
猜你喜欢
  • 2021-03-30
  • 2018-03-21
  • 1970-01-01
  • 2016-02-20
  • 2021-03-22
  • 2012-04-10
  • 2014-07-08
  • 2021-06-05
  • 2021-09-12
相关资源
最近更新 更多