Reputation: 439
I have a base URL file that includes all apps URL files, and what I am trying to do is on click of Generate PDF Button
, I want to hit an APIView / view-function
passing a variable along as parameter via get
method without encoding.
HTML:
<form onsubmit="{% url 'api-product:invoice-pdf-get' %}?R={{ variable }}">
<input type="submit" value="Generate PDF">
</form>
Base URL
path('api/product/', include(('store.urls', 'store'), namespace='api-product')),
path('invoice/', InvoiceUrl.as_view(), name='print-invoice'),
App URL:
path('invoice-pdf-get/', invoice.InvoiceToPdf.as_view(), name='invoice-pdf-get'),
On Click URL Generated: (Which is current, except with parameter)
http://localhost:8000/invoice/?
Can't understand why am I getting the same URL though when I inspect the HTML I see the URL included there, but without localhost:8000
.
There are several answers related to reverse URL on StackOverflow
, none helped.
Also I am not having any java script etc included, only bootstrap and a simple custom CSS. Have just a plain table.
If I just open the HTML from windows file directory, and click button I get:
http://localhost:63342/pos2all/templates/pogo-invoice.html?
UPDATED:
Now the parameter is not passed though still visible in HTML inspect, same way as above picture.
The API Looks Like:
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")
Upvotes: 1
Views: 587
Reputation: 23064
Use the form action
attribute instead of onsubmit
.
<form action="{% url 'api-product:invoice-pdf-get' %}">
The action
attribute is the url you want to form to be submitted to. But the form will add the data from the inputs as query parameters when you submit a form with the GET method, so you can't have a query string in the action url.
To pass query parameters with a form submission, use form inputs. You can use hidden
fields for data that the user is not supposed to edit.
<form action="{% url 'api-product:invoice-pdf-get' %}" >
<input type="hidden" name="R" value="{{ variable }}" />
<input type="submit" value="Generate PDF" />
</form>
The default method for form submission is GET
, but you can add the method explicitly to make this more clear.
<form method="get" action="{% url 'api-product:invoice-pdf-get' %}">
Upvotes: 4