python : name 'self' is not defined

0 views
Skip to first unread message

Abel Yyz via StackOverflow

unread,
Apr 20, 2016, 2:24:05 PM4/20/16
to google-appengin...@googlegroups.com

I got title's error with the following code. Does anyone know how to fix this? I already checked the tabs and spaces on the .py file and tried even changing var's names and it was useless. Thanks on advance

import os
import jinja2
import webapp2

JINJA_ENVIRONMENT = jinja2.Environment(
    loader=jinja2.FileSystemLoader(os.path.dirname(__file__)),
    extensions=["jinja2.ext.autoescape"],
    autoescape=True)

class ConsumoHandler(webapp2.RequestHandler):
     def load_input(self):
        self.km=float(self.request.get("edKm","edKm"))
        self.consmed=float(self.request.get("edCons","edCons"))
        self.tiempo=float(self.request.get("edTiempo","edTiempo"))
        self.velMed=0
        self.consTot=0

    def post(self):
        self.load_input()
        self.km=str(self.km)
        self.consmed=str(self.consmed)
        self.tiempo=str(self.tiempo)
        velMed=(self.km)/(self.tiempo)
        self.velMed=str(velMed)
        consTot=(self.km)/((self.consmed)*4)
        self.consTot=str(consTot)

        template_values = { 
        'kmToStr':self.km,
        'consmedToStr':self.consmed,
        'tiempoToStr':self.tiempo,
        'velMedToStr':self.velMed,
        'consTotToStr':self.consTot, 
        }

    template_values = JINJA_ENVIRONMENT.get_template("answer.html")
    self.response.write(template.render(template_values));

app = webapp2.WSGIApplication([
    ('/calcu', ConsumoHandler)
], debug=True)


Please DO NOT REPLY directly to this email but go to StackOverflow:
http://stackoverflow.com/questions/36751709/python-name-self-is-not-defined

Günther Jehle via StackOverflow

unread,
Apr 20, 2016, 2:24:07 PM4/20/16
to google-appengin...@googlegroups.com

Problem is this line:

self.response.write(template.render(template_values));

It's inside the class definition, but not inside a method (which first parameter is self. So self is not known here...



Please DO NOT REPLY directly to this email but go to StackOverflow:
http://stackoverflow.com/questions/36751709/python-name-self-is-not-defined/36752106#36752106

Günther Jehle via StackOverflow

unread,
Apr 20, 2016, 3:14:04 PM4/20/16
to google-appengin...@googlegroups.com

Problem is this line:

self.response.write(template.render(template_values));

It's inside the class definition, but not inside a method (which first parameter is self. So self is not known here...

You have to indent it to be part of the post method:

def post(self):
    ...
    template_values = { 
        ...
    }

    template_values = JINJA_ENVIRONMENT.get_template("answer.html")
    self.response.write(template.render(template_values));

trans1st0r via StackOverflow

unread,
Apr 20, 2016, 3:14:06 PM4/20/16
to google-appengin...@googlegroups.com

Firstly, the line self.response.write(template.render(template_values)); cannot be outside any function here, since there is no 'self' variable there.

Secondly, and this is a guess, based on the fact that you are overwriting the template_values variable, you probably wanted your post method to be like (note the last two lines indented and with a variable name changed):

def post(self):
    self.load_input()
    self.km=str(self.km)
    self.consmed=str(self.consmed)
    self.tiempo=str(self.tiempo)
    velMed=(self.km)/(self.tiempo)
    self.velMed=str(velMed)
    consTot=(self.km)/((self.consmed)*4)
    self.consTot=str(consTot)

    template_values = { 
    'kmToStr':self.km
,
    'consmedToStr':self.consmed,
    'tiempoToStr':self.tiempo,
    'velMedToStr':self.velMed,
    'consTotToStr':self.consTot, 
    }

    template = JINJA_ENVIRONMENT.get_template("answer.html") # Do not overwrite template_values, and use correct indentation
    self.response.write(template.render(template_values));


Please DO NOT REPLY directly to this email but go to StackOverflow:
http://stackoverflow.com/questions/36751709/python-name-self-is-not-defined/36752209#36752209
Reply all
Reply to author
Forward
0 new messages