Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django TypeError: get() got multiple values for keyword argument 'invoice_id'

Tags:

python

django

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

like image 545
Md. Tanvir Raihan Avatar asked Nov 21 '16 07:11

Md. Tanvir Raihan


2 Answers

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):
like image 156
Daniel Roseman Avatar answered Nov 05 '22 14:11

Daniel Roseman


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....

like image 5
boatcoder Avatar answered Nov 05 '22 16:11

boatcoder