使用python webapp2中的get方法获取表单数据

时间:2013-11-15 17:29:25

标签: python google-app-engine

我正在使用python开发一个Web应用程序,我想在getmethod中从我的表单中获取数据。我该怎么做呢?以下是我的代码:

class RedirectPage(webapp2.RequestHandler):
    def get(self):
        self.response.write('<html><body><h1> TJust a trial </h1>')
        self.response.write("""Enter your comment:
        <form method = "get" name = "frm">
        <input type = "textarea" id = "forms" name ="answered" value = "this"></input>
        <input type = "textarea" name ="unanswered" value = "this"></input>
        <input type = "textarea" name ="correct" value = "this"></input>
        <input type = "textarea" name ="wrong" value = "this"></input>
        <input type="submit"></input>
        </form><script type="text/javascript">
        var id = ""+window.location.hash;
        var details= id.substring(1,id.length);
        var user_details = new Array();
        user_details = details.split("gc");
        var answered = user_details[0];
        var unanswered = user_details[1];
        var correct = user_details[2];
        var wrong = user_details[3];
        document.frm.answered.value = answered;
        document.frm.unanswered.value = unanswered;
        document.frm.correct.value = correct;
        document.frm.wrong.value = wrong;
        </script>"""
        )

现在,从上面的代码中,我如何得到name="correct"中的值。我不想使用post方法,因为我想在执行上面的代码后重定向页面。

1 个答案:

答案 0 :(得分:0)

您可能在GAE中使用Webapp2。所以这应该有效:

基本设置是一个POST处理程序,它接收POST数据:

class MyHandler(webapp2.RequestHandler):
    def post(self):
        name = self.request.get('name')

如果您在提交表单中有一个名为“name”的值,则其内容将被分配给变量“name”。

同样:

# <input name="name" type="text" />
name = self.request.get("name")

# <input name="subscribe" type="checkbox" value="yes" />
subscribe_to_newsletter = self.request.get("subscribe", default_value="no")

# <select name="favorite_foods" multiple="true">...</select>
favorite_foods = self.request.get("favorite_foods", allow_multiple=True)

更多信息:http://webapp-improved.appspot.com/guide/request.html