django参数通过get不能在形成直接url时起作用

时间:2018-08-07 10:03:16

标签: django django-urls

基本URL:

path('api/product/',
include(('store.urls', 'store'),
namespace='api-product')),

商店网址:

path('invoice-pdf-get/',
invoice.InvoiceToPdf.as_view(),
name='invoice-pdf-get'),

HTML:

<html>
<body>
<form method="get" action="{% url 'api-product:invoice-pdf-get' %}?R={{ invoice.invoice_unique_number }}">
<input type="submit" value="Generate PDF">
</form>
</body>
</html>

当我按下按钮时,我在浏览器中得到的网址为:

http://localhost:8000/api/product/invoice-pdf-get/?

符合预期:

http://localhost:8000/api/product/invoice-pdf-get/?invoice_number=SOMEKEY

尽管如果我通过表单提交隐藏的类型输入,但会得到预期的结果
,但我正在阅读:Daniel Roseman SO answer。通过GET传递参数。
虽然inspect显示了URL(参见图片),但是为什么我没有得到预期的结果?

form url

1 个答案:

答案 0 :(得分:1)

通过GET提交表单时,表单中的值作为查询字符串发送。此{em>覆盖 action URL中的所有查询字符串。例如,请参见this SO answer

您应该将值作为隐藏的输入内容输入表单本身。

<form method="get" action="{% url 'api-product:invoice-pdf-get' %}">
  <input type="hidden" name="R" value="{{ invoice.invoice_unique_number }}">
  <input type="submit" value="Generate PDF">
</form>