我是编程的新手,所以很可能我做错了,但这就是我要做的。
我正在尝试编写一个程序,其中包含一个表单,该表单在一个类中接受用户输入,然后将其传递给另一个类,但我无法弄清楚如何做到这一点。这是相关代码的片段(我把 * 放在用户名变量周围,我试图传递的那个):
class MainHandler(webapp2.RequestHandler):
#this is in a form
def post(self):
***username*** = self.request.get('username')
#if username and other info entered correctly, then it redirects user to a new page and the WelcomeHandler class, so how do I get the username into the "Welcome, " message below?:
class WelcomeHandler(webapp2.RequestHandler):
def get(self):
self.response.out.write("Welcome, " + ***username***)
编辑:这是我的整个代码,如果它有用:
import webapp2
import re
import cgi
form="""
<form method = "post">
Signup
<br>
<table>
<tbody>
<tr><td><label>Username</label></td><td><input type="text" name = "username"
value="%(username)s"><div style="color: red">%(error)s</div> </td></tr>
<br>
<tr><td> <label>Password</label></td><td><input type="password" name = "password"
value="%(password)s"><div>TK TK TK</div></td></tr>
<br>
<tr><td> <label> Verify password</label></td><td><input type="password" name =
"verify" value="%(verify)s"><div> TK TK TK</div></td></tr>
<br>
<tr><td> <label> Email(optional)</label></td><td><input type = "text" name = "email"
value ="%(email)s"><div> TK TK TK</div></td></tr>
</tbody>
</table>
<br>
<br>
<input type = "submit">
</form>
"""
def escape_html(s):
return cgi.escape(s, quote = True)
def valid_username(username):
USER_RE = re.compile(r"^[a-zA-Z0-9_-]{3,20}$")
return USER_RE.match(username)
def valid_password(password):
USER_RE = re.compile(r"^.{3,20}$")
return USER_RE.match(password)
def verify_password(password, verify):
if verify == password:
return True
else:
return False
def valid_email(email):
USER_RE = re.compile(r"^[\S]+@[\S]+\.[\S]+$")
return USER_RE.match(email)
class MainHandler(webapp2.RequestHandler):
def write_form(self, error="", username="", password="", verify="", email=""):
self.response.out.write(form % {'error': error, "username":
escape_html(username), "password": escape_html(password), "verify": escape_html(verify),
"email": escape_html(email)})
def get(self):
self.write_form()
def post(self):
username = self.request.get('username')
password = self.request.get('password')
verify = self.request.get('verify')
email = self.request.get('email')
username_check = valid_username(username)
password_check = valid_password(password)
verify_check = verify_password(password, verify)
email_check = valid_email(email)
if not (username_check and password_check and verify_check):
self.write_form("That's not valid, you jive turkey", username, password,
verify, email)
else:
self.redirect("/welcome")
class WelcomeHandler(webapp2.RequestHandler):
def get(self):
self.response.out.write("Welcome, " + username)
app = webapp2.WSGIApplication([
('/', MainHandler),
('/welcome', WelcomeHandler)], debug=True)
答案 0 :(得分:4)
你不想这样做。这不仅仅是普通的基于类的编程:每个类都代表从浏览器到服务器的请求。在类之间传递数据意味着什么?
您实际想要做的是告诉浏览器重定向到您的GET处理程序。
答案 1 :(得分:1)
尝试在webapp2文档中查看setting cookies和reading cookies。
答案 2 :(得分:1)
丹尼尔所说的 - 您需要了解HTTP请求/响应周期以及您的RequestHandler 实例如何绑定它。
这里通常的解决方案是使用“会话” - 一种机制,它将数据从一个请求持久保存到另一个请求,通常由用户浏览器中存储为cookie的不透明密钥标识。 FWIW,任何体面的Web框架都应该为会话提供支持,所以你可能想在这里阅读你的框架文档。