Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django - missing 1 required positional argument: 'request'

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?

like image 600
Giorge Caique Avatar asked Aug 16 '17 17:08

Giorge Caique


2 Answers

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)
like image 189
Siegmeyer Avatar answered Sep 18 '22 08:09

Siegmeyer


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.

like image 38
Willemoes Avatar answered Sep 22 '22 08:09

Willemoes