django 反向 url 命中相同 url

django reverse url hitting same url

我有一个包含所有应用 URL 文件的基础 URL 文件,我想做的是点击 Generate PDF Button,我想点击 APIView / view-function 通过 get 方法将变量作为参数传递,无需编码。

HTML:

<form onsubmit="{% url 'api-product:invoice-pdf-get' %}?R={{ variable }}">
<input type="submit" value="Generate PDF">
</form>

基础URL

path('api/product/', include(('store.urls', 'store'), namespace='api-product')),
path('invoice/', InvoiceUrl.as_view(), name='print-invoice'),

应用URL:

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

单击 URL 生成:(当前,带参数除外)

http://localhost:8000/invoice/?



无法理解为什么我得到相同的 URL 虽然当我检查 HTML 时我看到 URL 包含在那里,但是 没有 localhost:8000Whosebug 上有几个与反向 URL 相关的答案,none 有所帮助。
此外,我没有包含任何 java 脚本等,只有 bootstrap 和一个简单的自定义 CSS。有一个简单的 table.

如果我只是 从 windows 文件目录打开 HTML, 并单击按钮 我得到:

http://localhost:63342/pos2all/templates/pogo-invoice.html?


更新:

现在参数没有传递,但在 HTML inspect 中仍然可见,与上图相同。


API 看起来像:

class InvoiceToPdf(APIView):
    """
    This API is used to get the Invoice and return pdf of invoice
    using rest_framework Response and  premission_classes
    """
    permission_classes = (AllowAny,)

    def get(self, request):
        return Response("hi")

使用表单 action 属性而不是 onsubmit

<form action="{% url 'api-product:invoice-pdf-get' %}">

action 属性是您要将表单提交到的 url。但是当您使用 GET 方法提交表单时,表单会将来自输入的数据添加为查询参数,因此您不能在操作中包含查询字符串 url.

要通过表单提交传递查询参数,请使用表单输入。您可以对用户不应编辑的数据使用 hidden 字段。

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

表单提交的默认方法是 GET,但您可以显式添加该方法以使其更加清晰。

<form method="get" action="{% url 'api-product:invoice-pdf-get' %}">