[英]Need to access self.request.GET from main.py in lib.py
I need to do calculations among other things within lib.py and I need to access the user input data within main.py for it, because it needs to be that way. 我需要在lib.py中进行其他计算,并且需要在main.py中访问用户输入数据,因为它需要那样。 How do I do this?
我该怎么做呢?
doing from main import MainHandler in lib.py and then calling it "works" in the sense that it doesn't give any code errors, but displays a blank page when done that way and I get a log error saying that I cannot import it. 从lib.py中的main import MainHandler进行操作,然后在不产生任何代码错误的情况下将其称为“工作”,但这样做后会显示空白页,并且出现日志错误,提示我无法导入它。
main.py main.py
import webapp2
from lib import FormData
from pages import FormPage
from pages import ResultsPage
class MainHandler(webapp2.RequestHandler):
def get(self):
f = FormPage()
s = ResultsPage()
fd1 = FormData()
if self.request.GET:
fd1.name = self.request.GET['name']
fd1.email = self.request.GET['email']
fd1.weight = self.request.GET['weight']
fd1.height = self.request.GET['height']
self.response.write(s.second_page(fd1.name, fd1.email, fd1.weight, fd1.height))
else:
self.response.write(f.form_page)
app = webapp2.WSGIApplication([
('/', MainHandler)
], debug=True)
lib.py lib.py
class FormData(object):
def __init__(self):
pass
I need to be able to access: 我需要能够访问:
fd1.name = self.request.GET['name']
fd1.email = self.request.GET['email']
fd1.weight = self.request.GET['weight']
fd1.height = self.request.GET['height']
In lib.py 在lib.py中
Your if/else statement is outside the get
method, so it shouldn't even work properly. 您的if / else语句不在
get
方法之外,因此它甚至不能正常工作。
I think the most clean way would be to pass the request data to the FormData
class already in the initialiser. 我认为最干净的方法是将请求数据传递给初始化器中已经存在的
FormData
类。 So the code would look like this: 因此,代码如下所示:
main.py main.py
import webapp2
from lib import FormData
from pages import FormPage
from pages import ResultsPage
class MainHandler(webapp2.RequestHandler):
def get(self):
f = FormPage()
s = ResultsPage()
fd1 = FormData(data=self.request.GET)
if fd1.name: # This is None, if form has not been submitted
self.response.write(s.second_page(fd1.name, fd1.email, fd1.weight, fd1.height))
else:
self.response.write(f.form_page)
app = webapp2.WSGIApplication([
('/', MainHandler)
], debug=True)
lib.py lib.py
class FormData(object):
def __init__(self, data):
self.name = data.get('name')
self.email = data.get('email')
self.weight = data.get('weight')
self.height = data.get('height')
As a side note, I don't really know this webapp2 framework, so there might be a better way to do this. 附带说明一下,我并不真正了解这个webapp2框架,因此可能会有更好的方法来做到这一点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.