I am relatively new in python and django,
i have following rest api view,
class InvoiceDownloadApiView(RetrieveAPIView):
"""
This API view will retrieve and send Terms and Condition file for download
"""
permission_classes = (IsAuthenticated,)
def get(self, invoice_id, *args, **kwargs):
if self.request.user.is_authenticated():
try:
invoice = InvoiceService(user=self.request.user, organization=self.request.organization).invoice_download(
invoice_id=invoice_id)
except ObjectDoesNotExist as e:
return Response(e.message, status=status.HTTP_404_NOT_FOUND)
if invoice:
response = HttpResponse(
invoice, content_type='application/pdf')
response['Content-Disposition'] = 'inline; filename={0}'.format(
invoice.name.split('/')[-1])
response['X-Sendfile'] = smart_str(invoice)
return response
else:
return Response({"data": "Empty File"}, status=status.HTTP_400_BAD_REQUEST)
using following urls,
urlpatterns = [
url(r'^invoice/(?P<invoice_id>[0-9]+)/download/$', views.InvoiceDownloadApiView.as_view()),
]
root url pattern is as following,
url(r'^api/payments/', include('payments.rest_api.urls', namespace="payments")),
when i call the endpoint,
localhost:8000/api/payments/invoice/2/download/
it arise the following error,
TypeError at /api/payments/invoice/2/download/
get() got multiple values for keyword argument 'invoice_id'
can't figure it out what actually causing this error
The first argument (after self
) to a view method is always request
. The way you've defined it, the request is being passed in as the invoice_id
method, and the actual invoice_id is being passed in as an additional kwarg, hence the error.
Define your method like this:
def get(self, request, invoice_id, *args, **kwargs):
You will also get the error if you define your method without self, like:
def get(request, token):
or
def post(request, token):
Like I did once....
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With