2017-08-16 127 views
1

試圖訪問該方法get_indiceComercioVarejista時,我得到的錯誤Django的 - 失蹤1個人需要位置參數:「請求」

get_indiceComercioVarejista() missing 1 required positional argument: '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) 
] 

有人可以幫助我,好嗎?

+0

你想要'.get'來做到這一點,並在你的網址中使用'views.ChartData.as_view()'...(或者如果你已經有了apiview的prepare/dispatch方法,不止一個取決於任何標準......) –

回答

0

request作爲第一個參數傳遞。你的第一個參數是self

這就是爲什麼它會是一個好主意,從ChartData類提取get_indiceComercioVarejista

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

仍然無法正常工作。 get方法傳遞self作爲第一個參數,它工作正常。 –

+0

您是否從課堂中提取了'get_indiceComercioVarejista'方法? 'get'方法可以工作,因爲你通過'as_view()'使用它, – Siegmeyer

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) 

另一種解決方案是使用ViewSet這與DRF工作時的推薦。

+0

沒有工作。錯誤:TypeError:as_view()需要1個位置參數,但2個被給出 –

+0

更新的答案,我以爲你使用ViewSets – Willemoes

相關問題