Django - 缺少1个必需的位置参数: 'request'

12

我在尝试访问get_indiceComercioVarejista方法时遇到了错误:

缺少1个必需的位置参数:'request'

我不知道出了什么问题。

视图:

from django.http import JsonResponse
from django.shortcuts import render, HttpResponse
import requests
import pandas as pd

from rest_framework.views import APIView
from rest_framework.response import Response

class ChartData(APIView):

    authentication_classes = []
    permission_classes = []

    def get(self, request, format=None):

         data = {
            'customer' : 10,
            'sales': 100
        }

        return Response(data)

    def get_indiceComercioVarejista(self, request, format=None):
        data = {
            'customer' : 10,
            'sales': 100
        }
        return Response(data)

网址:

from django.conf.urls import url
from . import views
from django.contrib.auth.views import login

urlpatterns = [
    url(r'^$', views.home),
    url(r'^login/$', login, {'template_name': 'Oraculum_Data/login.html'}),
    url(r'^cancerColo/$', views.cancerColo),
    url(r'^educacao/$', views.educacao),
    url(r'^comercio/$', views.comercio),
    url(r'^saude/$', views.saude),
    url(r'^api/chart/data/$', views.ChartData.as_view()),
    url(r'^api/chart/indiceVolumeReceitaComercioVarejista/$', views.ChartData.get_indiceComercioVarejista)
]

有人可以帮助我吗,请?


你需要使用.get来实现这个功能,并在你的urls中使用views.ChartData.as_view()...(或者如果你有多个get方法,可以在apiview的prepare/dispatch方法中选择合适的get方法,根据任何标准...) - Jon Clements
4个回答

7

request作为第一个参数传递。你的第一个参数是self

这就是为什么从ChartData类中提取get_indiceComercioVarejista是个好主意:

def get_indiceComercioVarejista(request, format=None):
    data = {
        'customer' : 10,
        'sales': 100
    }
    return Response(data)

1
仍然无法工作。get方法将self作为第一个参数传递,它可以正常工作。 - Giorge Caique
你从类中提取了 get_indiceComercioVarejista 方法吗?因为你是通过 as_view() 使用它,所以 get 方法会起作用。 - Siegmeyer

3

我认为最好的方法是将get_indiceComercioVarejista移出APIView,因为APIView只是分派到常规http方法:get post put patch delete

例如:

view.py

def get_indiceComercioVarejista(request, format=None):
    data = {
        'customer' : 10,
        'sales': 100
    }
    return Response(data)

urls.py

url(r'^api/chart/indiceVolumeReceitaComercioVarejista/$', views.get_indiceComercioVarejista)

另一个解决方案是使用ViewSet,这是在使用DRF时推荐的方法。

没成功。错误:TypeError: as_view()只需要一个位置参数,但是给了两个。 - Giorge Caique

1

对其他答案进行扩展:

您的视图将get_iniceComercioVarejista定义为ChartData类的实例方法。

然而,在您的urls.py中,您有以下行:

url(r'^api/chart/indiceVolumeReceitaComercioVarejista/$', views.ChartData.get_indiceComercioVarejista)

您必须声明一个 ChartData 的实例,通过添加括号使该行代码能够按照您当前编写的视图代码工作。修改后的代码应该如下:

url(r'^api/chart/indiceVolumeReceitaComercioVarejista/$', views.ChartData().get_indiceComercioVarejista)

一个备选方案是按照其他人的建议从方法定义中删除self,即:
def get_indiceComercioVarejista(request, format=None)

这种方法隐式地将get_indiceComercioVarejista转换为静态方法(请阅读此处的讨论),您的urls.py将按原样工作。如果您选择此解决方案,则强烈建议添加staticmethod装饰器。

最后,如果您决定将get_indiceComercioVarejista移出ChartData类,则删除self参数并使用@Willemoes的解决方案。


0

只需要删除 self

from django.http import JsonResponse
from django.shortcuts import render, HttpResponse
import requests
import pandas as pd

from rest_framework.views import APIView
from rest_framework.response import Response

class ChartData(APIView):

    authentication_classes = []
    permission_classes = []

    def get(self, request, format=None):

         data = {
            'customer' : 10,
            'sales': 100
        }

        return Response(data)


#just remove self from bellow function it's unnecessary

#before
    def get_indiceComercioVarejista(self, request, format=None):
        data = {
            'customer' : 10,
            'sales': 100
        }
        return Response(data)
      
#after
    def get_indiceComercioVarejista(request, format=None):
        data = {
            'customer' : 10,
            'sales': 100
        }
        return Response(data)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接