Reputation: 37
why it does not work ? It suppose to work, in python I can use this like this function, can any one explain me please ?
views:
class MiVista(View):
def get(self, request, var):
self.var = 'Hello'
# <la logica de la vista>
return HttpResponse(self.var)
one = MiVista()
one.get(222222)
urls:
url(r'^indice/', MiVista.as_view()),
So the functions does not work like function in python using POO ?
Thank you guys!
Upvotes: 0
Views: 42
Reputation: 23484
So as @MadWombat mentioned, you are not passing enough arguments, so you need to pass self
, which already passing by calling from instance objects, request
(not passing), var
(passing). And as you are not providing that you are passing var=2222
, python thinks that 2222
is request
argument.
So basically you need to create request
argument. You can do this with RequestFactory
. Like that
from django.test import RequestFactory
from django.views.generic import View
class MiVista(View):
def get(self, request, var):
self.var = var
# <la logica de la vista>
return HttpResponse(self.var)
rf = RequestFactory()
rf.get('indice/')
one = MiVista.as_view()(rf, var='hello')
Upvotes: 1