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

时间:2017-08-16 17:51:25

标签: python django web django-rest-framework

我收到了错误

  

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

有人可以帮助我吗?

2 个答案:

答案 0 :(得分:0)

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

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

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

答案 1 :(得分:0)

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

e.g:

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

另一种解决方案是在使用DRF时使用ViewSet