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

django parameter via get not working while forming direct url

基础URL:

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

店铺URL:

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>

当我按下按钮时,我在浏览器中得到 url 作为:

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

如预期:

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

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

当通过 GET 提交表单时,表单中的值将作为查询字符串发送。此 覆盖 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>