【问题标题】:Python - Text in form disappearsPython - 表单中的文本消失
【发布时间】:2014-11-06 08:52:57
【问题描述】:

在下面的代码中,我想在按下提交按钮时将文本留在表单中。目前按下提交时文本会消失。

这是我一直关注的教程链接: https://www.udacity.com/course/viewer#!/c-cs253/l-48736183/e-48754026/m-48717294

import webapp2

form = """
<!DOCTYPE html>
    <head>
        <title>Unit 2 Rot 13</title>
    </head>
    <body>
        <h2>Enter some text to ROT13:</h2>
        <form method = "post">
            <textarea name="text" value="%(word)s" style="height: 100px; width: 400px;"></textarea>
            <br>
            <input type="submit">
        </form>
    </body>
</html>

"""

class MainHandler(webapp2.RequestHandler):
    def write_form(self, word=""):
        self.response.out.write(form % {"word": word})

    def get(self):
        self.write_form()

    def post(self):
        string = self.request.get("word")
        self.write_form(string)


app = webapp2.WSGIApplication([('/', MainHandler),
], debug=True)

【问题讨论】:

    标签: python html forms


    【解决方案1】:
    string = self.request.get("text")
    

    因为您的表单项名称是text

    这让人联想到您应该使用良好的标识符......

    因为您没有名为 word 的 post 变量...尝试获取该 post 变量会给出一个空字符串

    【讨论】:

      猜你喜欢
      • 2014-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-24
      • 1970-01-01
      • 2012-06-17
      • 2010-12-10
      相关资源
      最近更新 更多