python - django 类型错误 : get() got multiple values for keyword argument 'invoice_id'

标签 python django

我对 python 和 django 比较陌生,

我有以下 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)

使用以下网址,

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'

无法弄清楚到底是什么导致了这个错误

最佳答案

View 方法的第一个参数(在 self 之后)始终是 request。按照您定义它的方式,请求作为 invoice_id 方法传入,而实际的 invoice_id 作为附加 kwarg 传入,因此出现错误。

像这样定义你的方法:

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

关于python - django 类型错误 : get() got multiple values for keyword argument 'invoice_id' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40714495/

相关文章:

python - 如何使用 matplotlib 更新图形

django - Django + Tastypie 分析

python - 如何在请求中流式传输文件?

python - 从 Django View 构建 HTML

python - Django 管理员 : TypeError: __str__ returned non-string (type FieldFile)

python - 缺少依赖项在 Windows 7 上为 Python 3.4.1 32 位安装 NumPy 1.9

python - 如何将变量文件名传递给 python ete?

python - python 2.7+ 中的组引用无效

python - 使用循环从 PyQt4 中 QListView 中的 QStandartItemModel 中删除 QStandardItems

python - 输入记录与 python mapreduce 中的输出记录不匹配