Django - missing 1 required positional argument:'请求'

4ngedf3f  于 2023-05-23  发布在  Go
关注(0)|答案(4)|浏览(354)

我得到了错误
get_indiceComercioVarejista()缺少1个必需的位置参数:'请求'
当尝试访问方法get_indiceComercioVarejista时。我不知道它有什么问题。
观点:

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)
]

有人能帮帮我吗

r6l8ljro

r6l8ljro1#

request作为第一个参数传递。第一个参数是self
这就是为什么从ChartData类中提取get_indiceComercioVarejista是一个好主意:

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

jpfvwuh42#

我认为最好的方法是将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)

url.py

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

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

c2e8gylq

c2e8gylq3#

扩展其他答案:
您的视图将get_iniceComercioVarejista定义为ChartData类的示例方法。
但是,在您的www.example.com中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转换为一个静态方法(请阅读here的讨论),并且您的urls.py将按照编写的那样工作。如果您选择这种解决方案,我强烈建议添加一个staticmethod装饰器。
最后,如果您决定将get_indiceComercioVarejista移出ChartData类,则删除self参数并使用@Willemoes中的解决方案

zmeyuzjn

zmeyuzjn4#

只删除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)

相关问题