我是编程新手,所以很有可能我做错了,但这就是我想要做的。
我正在尝试编写一个程序,该程序具有一个表单,该表单在一个类中接受用户输入,然后将其传递给另一个类,但我不知道该怎么做。这是相关代码的片段(我将*放在用户名变量周围,我试图传递的那个):
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)