13

I'm getting the error

get_indiceComercioVarejista() missing 1 required positional argument: 'request'

when trying to access the method get_indiceComercioVarejista. I don't know what it's wrong with it.

views:

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)

urls:

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

Can someone help me, please?

1
  • You want .get to do that and use views.ChartData.as_view() in your urls... (or have the prepare/dispatch method for the apiview pick the appropriate get method if you've got more than one depending on whatever criteria...) Commented Aug 16, 2017 at 17:55

4 Answers 4

9

request is passed as a first argument. Your first argument is self.

This is why it would be a good idea to extract get_indiceComercioVarejista from ChartData class:

def get_indiceComercioVarejista(request, format=None):
    data = {
        'customer' : 10,
        'sales': 100
    }
    return Response(data)
Sign up to request clarification or add additional context in comments.

2 Comments

still not working. The get method is passing self as the first argument and it works fine.
Did you extract get_indiceComercioVarejista method from the class? get method will work because you're using it via as_view(),
3

I think the best approach would be to move get_indiceComercioVarejista out of the APIView, because APIView just dispatchs to the regular http methods: 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)

Another solution would be to use ViewSet which are the recommended when working with DRF.

1 Comment

Didn't work. Error: TypeError: as_view() takes 1 positional argument but 2 were given
1

Expanding on the other answers:

Your view defines get_iniceComercioVarejista as an instance method of the ChartData class.

However, in your urls.py, you have the following line:

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

You must declare an instance of ChartData by adding parentheses for that line to work as your view code is currently written. The modified line should read:

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

An alternative solution is removing self from the method definition as others have suggested, i.e.:

def get_indiceComercioVarejista(request, format=None)

This approach implicitly turns get_indiceComercioVarejista into a static method (read the discussion here) and your urls.py would work as written. If you choose this solution, I strongly suggest adding a staticmethod decorator.

Finally, if you decide to move get_indiceComercioVarejista out of the ChartData class, then remove the self argument and use the solution from @Willemoes

Comments

0

just remove 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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.