【问题标题】:Display the same page even after form submission即使在表单提交后也显示相同的页面
【发布时间】:2015-12-12 18:25:32
【问题描述】:

在下面的简单表单提交中,我输入了一个名称,它在提交表单后打印一条消息(“我的意思是表单消失了,只显示消息”),但我想显示相同的消息连同表格

enter code here
import tornado.ioloop
import tornado.web

class Main(tornado.web.RequestHandler):
    def get(self):
            self.render("auth.html")
    def post(self):
            ab=self.get_body_argument("username","")
            self.write(ab+" printing in a new page")


application = tornado.web.Application([
    (r"/", Main),
    ],debug=True,)

if __name__ == "__main__":
application.listen(8054)
tornado.ioloop.IOLoop.instance().start()

为了您的方便,这里是 HTML 页面:

<html>

<head>
Welcome
</head>
<body>
    <div id="complete">

        <div id="form">
            <form action="/" method="post">
                <input type= "text" name="username" >
                <input type="submit" value="Login">
            </form>
        </div>
    </div>
</body>
</html>

【问题讨论】:

  • 从表单操作中删除“/”它将重新加载同一页面
  • @Farhan no bro 我试过了,我认为它在提交表单后再次刷新,因此只打印我想要的 self.write ("Message") 以及表单字段

标签: html tornado


【解决方案1】:

您有两个独立的 POST 和 GET 处理程序,但只有在 GET 中您才真正呈现表单。接下来,将writerender 一起使用不是一个好主意 - 它会起作用,但文本将在 HTML 之外(之前或之后),解决方案 - 模板变量。

import tornado.ioloop
import tornado.web

class Main(tornado.web.RequestHandler):
    def get(self):
        self.render("auth.html", message=None)

    def post(self):
        msg = "{} printing in a new page".format(
            self.get_body_argument("username","")
        )
        self.render("auth.html", message=msg)


application = tornado.web.Application([
    (r"/", Main),
    ],debug=True,)

if __name__ == "__main__":
    application.listen(8054)
    tornado.ioloop.IOLoop.instance().start()

还有 auth.py

<html>

<head>
Welcome
</head>
<body>
    <div id="complete">

        <div id="form">
            {% if message is not None %}
            <p>{{ message }}</p>
            {% end %}
            <form action="/" method="post">
                <input type= "text" name="username" >
                <input type="submit" value="Login">
            </form>
        </div>
    </div>
</body>
</html>

有关龙卷风模板的更多信息:http://tornadokevinlee.readthedocs.org/en/latest/template.html

【讨论】:

    猜你喜欢
    • 2013-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多