django TypeError:get()得到了关键字参数的多个值' invoice_id'

时间:2016-11-21 07:00:25

标签: python django

我在python和django中比较新,

我有以下休息api视图,

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)

使用以下网址

urlpatterns = [
    url(r'^invoice/(?P<invoice_id>[0-9]+)/download/$', views.InvoiceDownloadApiView.as_view()),

root url模式如下,

url(r'^api/payments/', include('payments.rest_api.urls', namespace="payments")),   

当我呼叫端点时,

localhost:8000/api/payments/invoice/2/download/

出现以下错误,

TypeError at /api/payments/invoice/2/download/
get() got multiple values for keyword argument 'invoice_id'

无法弄清楚实际导致此错误的原因

2 个答案:

答案 0 :(得分:24)

视图方法的第一个参数(在self之后)始终为request。您定义它的方式是,请求作为invoice_id方法传入,实际的invoice_id作为额外的kwarg传入,因此错误。

像这样定义你的方法:

def get(self, request, invoice_id, *args, **kwargs):

答案 1 :(得分:1)

如果您使用self定义方法,也会收到错误,例如:

def get(request, token):

def post(request, token):

就像我做过一次......