我在运行下面的代码时遇到麻烦。
代码应该可以工作(这是针对问题集的Udacity源代码解决方案)。它应在Google App Engine上运行。
在我的终端中,我导航到hw2文件夹,其中包含以下文件,位于〜/ python-docs-samples / appengine / standard
然后我跑
dev_appserver.py .
但是,当我在localhost:8080加载页面时出现404错误
适用于〜/ python-docs-samples / appengine / standard
中的“ Hello World”示例我在这里做错了什么?谢谢
import os
import re
from string import letters
import webapp2
import jinja2
from google.appengine.ext import db
template_dir = os.path.join(os.path.dirname(__file__), 'templates')
jinja_env = jinja2.Environment(loader = jinja2.FileSystemLoader(template_dir),
autoescape = True)
def render_str(template, **params):
t = jinja_env.get_template(template)
return t.render(params)
class BaseHandler(webapp2.RequestHandler):
def render(self, template, **kw):
self.response.out.write(render_str(template, **kw))
def write(self, *a, **kw):
self.response.out.write(*a, **kw)
class Rot13(BaseHandler):
def get(self):
self.render('rot13-form.html')
def post(self):
rot13 = ''
text = self.request.get('text')
if text:
rot13 = text.encode('rot13')
self.render('rot13-form.html', text = rot13)
USER_RE = re.compile(r"^[a-zA-Z0-9_-]{3,20}$")
def valid_username(username):
return username and USER_RE.match(username)
PASS_RE = re.compile(r"^.{3,20}$")
def valid_password(password):
return password and PASS_RE.match(password)
EMAIL_RE = re.compile(r'^[\S]+@[\S]+\.[\S]+$')
def valid_email(email):
return not email or EMAIL_RE.match(email)
class Signup(BaseHandler):
def get(self):
self.render("signup-form.html")
def post(self):
have_error = False
username = self.request.get('username')
password = self.request.get('password')
verify = self.request.get('verify')
email = self.request.get('email')
params = dict(username = username,
email = email)
if not valid_username(username):
params['error_username'] = "That's not a valid username."
have_error = True
if not valid_password(password):
params['error_password'] = "That wasn't a valid password."
have_error = True
elif password != verify:
params['error_verify'] = "Your passwords didn't match."
have_error = True
if not valid_email(email):
params['error_email'] = "That's not a valid email."
have_error = True
if have_error:
self.render('signup-form.html', **params)
else:
self.redirect('/unit2/welcome?username=' + username)
class Welcome(BaseHandler):
def get(self):
username = self.request.get('username')
if valid_username(username):
self.render('welcome.html', username = username)
else:
self.redirect('/unit2/signup')
app = webapp2.WSGIApplication([('/unit2/rot13', Rot13),
('/unit2/signup', Signup),
('/unit2/welcome', Welcome)],
debug=True)
答案 0 :(得分:1)
以下代码为相应的URL模式定义了请求处理程序:
app = webapp2.WSGIApplication([('/unit2/rot13', Rot13),
('/unit2/signup', Signup),
('/unit2/welcome', Welcome)],
debug=True)
请注意,您没有/
处理程序或全部捕获处理程序。这就是为什么您收到HTTP 404
错误的原因。