0

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!

1
  • 2
    Yes, they do. But just like any other function in python you need to pass right arguments. In your case get() takes two arguments and you are passing it one. And on top of that you are not passing the second argument in your urls. Commented Sep 30, 2016 at 21:22

1 Answer 1

1

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')
Sign up to request clarification or add additional context in comments.

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.