【问题标题】:Post method in webapp2 cannot use value from getwebapp2 中的 Post 方法不能使用 get 中的值
【发布时间】: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)

【问题讨论】:

  • 这个可以在官方关闭原因下关闭:这个问题是由一个无法再复制的问题或一个简单的印刷错误引起的。虽然类似的问题可能是这里的主题,但这个问题的解决方式不太可能帮助未来的读者

标签: python http webapp2


【解决方案1】:

您的 html 公司中的字段称为文本,而不是内容。

【讨论】:

    猜你喜欢
    • 2014-09-13
    • 1970-01-01
    • 2021-08-31
    • 2015-03-30
    • 1970-01-01
    • 2016-08-10
    • 2013-01-12
    • 1970-01-01
    • 2015-09-13
    相关资源
    最近更新 更多