Google App Engine:提取所选单选按钮的值

时间:2014-07-09 20:32:44

标签: python google-app-engine

我正在构建一个应用程序,允许用户创建调查并对调查问题进行投票。现在,这些问题的答案将存储在数据存储区中,以便我可以最终分析结果。但是,我无法找到提取用户选择的单选按钮值的方法。每个问题都有三个选项,所有选项都由一个单选按钮代表。

以下是代码:

main.py

import os
import webapp2
import jinja2
from google.appengine.ext import db
from random import randint

template_dir = os.path.join(os.path.dirname(__file__), 'templates')
jinja_env = jinja2.Environment(loader = jinja2.FileSystemLoader(template_dir), autoescape=True)

class Handler(webapp2.RequestHandler):
    def write(self, *a, **kw):
        self.response.out.write(*a, **kw)
    def render_str(self, template, **params):
        t = jinja_env.get_template(template)
        return t.render(params)
    def render(self, template, **kw):
        self.write(self.render_str(template, **kw))

#databases

class Survey(db.Model):
    vam_id = db.StringProperty()
    subject = db.StringProperty(required = True)
    description = db.TextProperty(required = True)
    created = db.DateTimeProperty(auto_now_add = True)
    q1 = db.TextProperty(required = True)
    o11 = db.TextProperty(required = True)
    o12 = db.TextProperty(required = True)
    o13 = db.TextProperty(required = True)
    q2 = db.TextProperty(required = True)
    o21 = db.TextProperty(required = True)
    o22 = db.TextProperty(required = True)
    o23 = db.TextProperty(required = True)
    q3 = db.TextProperty(required = True)
    o31 = db.TextProperty(required = True)
    o32 = db.TextProperty(required = True)
    o33 = db.TextProperty(required = True)
    q4 = db.TextProperty(required = True)
    o41 = db.TextProperty(required = True)
    o42 = db.TextProperty(required = True)
    o43 = db.TextProperty(required = True) 
    q5 = db.TextProperty(required = True)
    o51 = db.TextProperty(required = True)
    o52 = db.TextProperty(required = True)
    o53 = db.TextProperty(required = True)

class Votes(db.Model):
    vam_id = db.StringProperty(required = True)
    q1_opt = db.TextProperty(required = True)
    q2_opt = db.TextProperty(required = True)
    q3_opt = db.TextProperty(required = True)
    q4_opt = db.TextProperty(required = True)
    q5_opt = db.TextProperty(required = True)



class MainPage(Handler):
    def get(self):
        self.render("homepage.html")

class NewSurvey(Handler):
    def get(self):
        self.render("newsurvey.html")
    def post(self):
        vam_id = str(randint(1, 500))
        subject = self.request.get("title")
        description = self.request.get("description")
        q1 = self.request.get("q1")
        o11 = self.request.get("o11")
        o12 = self.request.get("o12")
        o13 = self.request.get("o13")
        q2 = self.request.get("q2")
        o21 = self.request.get("o21")
        o22 = self.request.get("o22")
        o23 = self.request.get("o23")
        q3 = self.request.get("q3")
        o31 = self.request.get("o31")
        o32 = self.request.get("o32")
        o33 = self.request.get("o33")
        q4 = self.request.get("q4")
        o41 = self.request.get("o41")
        o42 = self.request.get("o42")
        o43 = self.request.get("o43") 
        q5 = self.request.get("q5")
        o51 = self.request.get("o51")
        o52 = self.request.get("o52")
        o53 = self.request.get("o53")

        a = Survey(vam_id = vam_id, subject = subject, description = description, q1 = q1,
                    o11 = o11, o12 = o12, o13 = o13, q2 = q2,
                    o21 = o21, o22 = o22, o23 = o23, q3 = q3,
                    o31 = o31, o32 = o32, o33 = o33, q4 = q4,
                    o41 = o41, o42 = o42, o43 = o43, q5 = q5,
                    o51 = o51, o52 = o52, o53 = o53)
        a.put()            

class Existing(Handler):
    def get(self):
        surveys = Survey.all()
        self.render("existingsurvey.html", surveys = surveys)

class PermaSurvey(Handler):
    def get(self):
        vam = self.request.get("vam_id")
        su = Survey.all().filter("vam_id =", vam).get()
        self.render("perma.html", su = su)
    def post(self):
        vam = self.request.get("vam_id")
        q1_opt = self.request.get("q1")
        q2_opt = self.request.get("q2")
        q3_opt = self.request.get("q3")
        q4_opt = self.request.get("q4")
        q5_opt = self.request.get("q5")

        v = Votes(vam_id = vam, q1_opt = q1_opt, q2_opt = q2_opt, q3_opt = q3_opt,
                    q4_opt = q4_opt, q5_opt = q5_opt)  
        v.put()     

app = webapp2.WSGIApplication([('/', MainPage),
                               ('/new', NewSurvey),
                               ('/existing', Existing),
                               ('/perma', PermaSurvey)], debug=True)

perma.html

<html>
<head>
    <title>Perma Survey</title>
</head>
<body>
<form method = "post">
    {% block content %}
        <h2>{{su.subject}}</h2>
        <p>{{su.description}}</p>
        <br>
        {{su.q1}}
        <br>
        <input type="radio" name="q1">{{su.o11}}<br>
        <input type="radio" name="q1">{{su.o12}}<br>
        <input type="radio" name="q1">{{su.o13}}
        <hr>
        {{su.q2}}
        <br>
        <input type="radio" name="q2">{{su.o21}}<br>
        <input type="radio" name="q2">{{su.o22}}<br>
        <input type="radio" name="q2">{{su.o23}}
        <hr>
        {{su.q3}}
        <br>
        <input type="radio" name="q3">{{su.o31}}<br>
        <input type="radio" name="q3">{{su.o32}}<br>
        <input type="radio" name="q3">{{su.o33}}
        <hr>
        {{su.q4}}
        <br>
        <input type="radio" name="q4">{{su.o41}}<br>
        <input type="radio" name="q4">{{su.o42}}<br>
        <input type="radio" name="q4">{{su.o43}}
        <hr>
        {{su.q5}}
        <br>
        <input type="radio" name="q5">{{su.o51}}<br>
        <input type="radio" name="q5">{{su.o52}}<br>
        <input type="radio" name="q5">{{su.o53}}
        <hr>
        <br>
        <input type = "submit">
    {% endblock %}
</form>    
</body>
</html>

现在,无论我从列出的三个选项中选择哪个选项,数据存储区中存储的值都是“on”,表示选中了单选按钮组中的单选按钮。如何将用户选择的实际选项存储到数据存储区中?

1 个答案:

答案 0 :(得分:2)

您尚未为表单元素分配任何value个。我想你想要:

<input type="radio" name="q3" value="{{su.o31}}" />{{su.o31}}<br>

现在,您可以检索value。由于你已经在使用jinja,我建议你使用WTForms软件包来简化这一过程。