单选按钮和webapp2

时间:2018-10-01 10:47:26

标签: python html webapp2

下面是 quiz.html 文件中的表单。 在提交表单并写入/ testform后,我不知道如何从单击的按钮中获取值。

<html>
    <form method="post"  action="/testform">
        Q1. where are you from?<br>
        <input type = "radio" name = "q_1" value ="a">Iraklio</br>
        <input type = "radio" name = "q_1" value = "b">Patra</br>
        <input type = "radio" name = "q_1" value = "c">Athens</br>
        <input type = "submit" value = "submit" >
    </form>
</html>

Python脚本:

import webapp2
import jinja2


templateLoader = jinja2.FileSystemLoader( searchpath="./")
templateEnv = jinja2.Environment( loader=templateLoader )


class quiz(webapp2.RequestHandler):
        def get(self):
                 TEMPLATE_FILE = "quiz.html"
                 template = templateEnv.get_template( TEMPLATE_FILE )
                 x = template.render()
                 self.response.write(x)

class test(webapp2.RequestHandler):
        def post(self):
                ans = self.request.get('value')
                print ans
                self.response.write(ans)

app = webapp2.WSGIApplication([('/',quiz),('/testform',test)]
,debug = True)

def main():
        from paste import httpserver
        httpserver.serve(app, host='127.0.0.1', port='8080')

if __name__ == '__main__':
    main()

1 个答案:

答案 0 :(得分:0)

我的目标是: 提交帖子表格后,在子页面(/ testform)中打印单击的按钮的字母。

我的错误是在ans = self.request.get('value')中,我不得不将'value'替换为'q_1'。然后它可以正常工作!!

p.s。 https://webapp2.readthedocs.io/en/latest/guide/request.html