【发布时间】:2014-02-21 13:59:20
【问题描述】:
在下面的这段代码中,我使用 webapp2 从 udacity.com 学习网络编程,这是练习之一。我在这里想要实现的是呈现用户可以输入一些值的表单,并且在点击提交按钮后,用户应该在表单本身中看到他们加密的 rot13 数据。
每当我点击提交按钮时我都会遇到这个问题我的表单结果是空白并且用户数据丢失了,我使用 pdb 到 post 方法中的 self.request.get("content") 并且它变成了出来是空的。
import webapp2
import cgi
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; ">
%(content)s
</textarea>
<br>
<input type="submit">
</form>
</body>
</html>
"""
class MainPage(webapp2.RequestHandler):
def write_form(self, content = ""):
self.response.out.write(form %{"content": escape_html(content)
})
def get(self):
self.write_form()
def post(self):
content = self.request.get("content").encode("rot13")
self.write_form(content)
app = webapp2.WSGIApplication([('/', MainPage)],debug=True)
【问题讨论】:
-
这个可以在官方关闭原因下关闭:这个问题是由一个无法再复制的问题或一个简单的印刷错误引起的。虽然类似的问题可能是这里的主题,但这个问题的解决方式不太可能帮助未来的读者。