[Fixed]-Django/Python: How to call function from parent class within child class?

1👍

Your code seems to be fine, except for the fact that the print statement isn’t indented properly, and therefore is not part of Calculator() There’s also no point of adding self and request to the Main methods.

views.py

class Main(object):
    template = ""
    favorite_number = None
    least_favorite_number = None

    def add(a, b):
        return a + b

    def subtract(a, b):
        return a - b

    def multiply(a, b):
        return a * b

    def divide(a, b):
        return a / b

class Calculator(Main, View):
    template = 'calculator/index.html'
    favorite_number = 20
    least_favorite_number = 2

    def get(self, request):
        print self.add(self.favorite_number, self.least_favorite_number)
        return render(request, self.template, {})

Keep in mind that in order for you to see the print statement in your console, you have to actually be looking at the view. So wire in the Calculator view into urls.py, navigate to the page, and you should be able to see if the print statement is working in the console.

👤Hybrid

Leave a comment