我正在使用Google App Engine上的WebApp2学习网络编程,并遵循有关Udacity的教程(归档,因此学生社区不活跃。)有人可以告诉我为什么我为什么要开始用户名未在我的代码中定义。
Traceback (most recent call last):
File "/home/tekforce/Desktop/google_appengine/google/appengine/runtime/wsgi.py", line 239, in Handle
handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
File "/home/tekforce/Desktop/google_appengine/google/appengine/runtime/wsgi.py", line 298, in _LoadHandler
handler, path, err = LoadObject(self._handler)
File "/home/tekforce/Desktop/google_appengine/google/appengine/runtime/wsgi.py", line 84, in LoadObject
obj = __import__(path[0])
File "/home/tekforce/Desktop/helloUdacity/hello-udacity.py", line 40, in <module>
class LoginPage(webapp2.RequestHandler):
File "/home/tekforce/Desktop/helloUdacity/hello-udacity.py", line 66, in LoginPage
if not valid_username(username):
NameError: name 'username' is not defined
INFO 2014-02-03 22:07:26,452 module.py:612] default: "GET / HTTP/1.1" 500 -
以下是我的python代码,我正在尝试执行。
import webapp2
import cgi
import re
def escape_html(s):
return cgi.escape(s, quote=True)
form = """
<!DOCTYPE html>
<html>
<head>
<title>Unit 2 Rot 13</title>
</head>
<body>
<h2>Enter some text to ROT13:</h2>
<form method="post">
<textarea name="text" style="height: 100px; width: 400px; ">
%(text)s
</textarea>
<br>
<input type="submit">
</form>
</body>
</html>
"""
class MainPage(webapp2.RequestHandler):
def write_form(self, text = ''):
self.response.out.write(form % {'text': escape_html(text)})
def get(self):
self.write_form()
def post(self):
content = self.request.get('text').encode('rot13')
self.write_form(content)
class LoginPage(webapp2.RequestHandler):
USER_RE = re.compile(r"^[a-zA-Z0-9_-]{3,20}$")
PASSWORD_RE = re.compile(r"^.{3,20}$")
EMAIL_RE = re.compile(r"^[S]+@[S]+.[S]+$")
@classmethod
def valid_username(self,username):
return username and LoginPage.USER_RE.match(username)
@classmethod
def valid_password(self,password):
return password and LoginPage.PASSWORD_RE.match(password)
@classmethod
def valid_email(self, email):
return not email or LoginPage.EMAIL_RE.match(email)
def get(self):
self.render("signup.html")
def post(self):
hasError = False
username = self.request.get('username')
password = self.request.get('password')
verify = self.request.get('verify')
email = self.request.get('email')
# These params are send back to rendering,
params = dict(username = username, email = email)
if not valid_username(username):
params['error_username'] = "Thats not a valid user name."
hasError = True
if not valid_password(password):
params['error_username'] = "Thats not a valid password."
hasError = True
elif valid_password(password) != verify:
params['error_verify'] = "Password do not match."
hasError = True
if not valid_email(email):
params['error_email'] = "Thats not a valid email"
hasError = True
if hasError:
self.render("signup.html",**params)
else:
self.redirect('/welcome?username='+username)
class Welcome(BaseHandler):
def get(self):
username = self.request.get('username')
if LoginForm.valid_username(username):
self.render('welcome.html',username = username)
else:
self.redirect('/sign')
app = webapp2.WSGIApplication([('/', MainPage),
('/sign', LoginPage),
('/welcome',welcome)],
debug=True)
下面是我的Ingip.html代码。
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Sign Up</title>
<style type="text/css">
.label {text-align: right}
.error {color: red}
</style>
</head>
<body>
<h2>Signup</h2>
<form method="post">
<table>
<tbody><tr>
<td class="label">
Username
</td>
<td>
<input type="text" name="username" value="{{username}}">
</td>
<td class="error">
{{error_username}}
</td>
</tr>
<tr>
<td class="label">
Password
</td>
<td>
<input type="password" name="password" value="">
</td>
<td class="error">
{{error_password}}
</td>
</tr>
<tr>
<td class="label">
Verify Password
</td>
<td>
<input type="password" name="verify" value="">
</td>
<td class="error">
{{error_verify}}
</td>
</tr>
<tr>
<td class="label">
Email (optional)
</td>
<td>
<input type="text" name="email" value="{{email}}">
</td>
<td class="error">
{{error_email}}
</td>
</tr>
</tbody></table>
<input type="submit">
</form>
</body>
</html>
看来您的代码被误导了(从以下行)
if not valid_username(username):
将您的代码置于一个级别(以便属于post
函数),您应该摆脱问题。