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)
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...
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));
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));