Reputation: 5540
I'm using inheritance in my code which is running on main.py file on the Google App Engine. In my parent class, I do a bunch of computations and store the results in a bunch of variables. Now, I want to use those variables in my child class, and thus I inherited the parent class in order to access those parent variables.
Parent class
class MainPage(Handler):
def get(self):
self.render("indexab.html")
def post(self):
vam_id = str(randint(1, 5000))
name = self.request.get("name")
clas = self.request.get("clas")
roll_no = int(self.request.get("roll_no"))
semester = int(self.request.get("semester"))
q1 = int(self.request.get("1"))
q2 = int(self.request.get("2"))
q3 = int(self.request.get("3"))
q4 = int(self.request.get("4"))
q5 = int(self.request.get("5"))
q6 = int(self.request.get("6"))
q7 = int(self.request.get("7"))
q8 = int(self.request.get("8"))
q9 = int(self.request.get("9"))
total = Survey.all().count()
q1sea2 = Survey.all().filter("clas = ","SE-A").filter("q1 = ", 12).count()
q1sea4 = Survey.all().filter("clas = ","SE-A").filter("q1 = ", 14).count()
q1sea6 = Survey.all().filter("clas = ","SE-A").filter("q1 = ", 16).count()
q1sea8 = Survey.all().filter("clas = ","SE-A").filter("q1 = ", 18).count()
q1sea0 = Survey.all().filter("clas = ","SE-A").filter("q1 = ", 10).count()
q1sea = float((q1sea2*2 + q1sea4*4 + q1sea6*6 + q1sea8*8 + q1sea0*10))/float(total)
#some more code
self.render("thanx.html")
Child class
class AdminPage(MainPage):
def get(self):
self.render("admin1.html", q1sea = q1sea)
However, I get the following error in my log:
self.render("admin1.html", q1sea = q1sea)
NameError: global name 'q1sea' is not defined
What seems to be wrong with my code?
Upvotes: 0
Views: 323
Reputation: 9599
It should be self.q1sea = ...
instead of single q1sea = ...
. Otherwise, the garbage collector will delete the reference when post()
returns.
Also, you need to call MainPage.post()
before using MainPage.q1sea
in your child class.
That's the theory. In practice, as @DanielRoseman said, you wouldn't want to do that, use the database instead to store your data and load it later.
Upvotes: 0
Reputation: 599600
What you're trying to do makes no sense at all. The post and get are on completely different requests. Even if you could persist data between then like that, you wouldn't want to because all other users would also get access to that data.
You need to store the data somewhere between requests - either in the database, or in the session.
Upvotes: 4