GAE Python self.request.get_all不返回选定的值

时间:2017-06-15 11:19:19

标签: python google-app-engine select

我试图从SELECT标签中获取一个值。

我的HTML

   class SearchModal extends React.Component {
  render () {
    return (

      <View style={{ position: "absolute", left: 0, top: 0, bottom: 0, right: 0, flex: 1, backgroundColor: '#000', opacity: 0.5, display: "flex"}}>
        <TextInput
          style={{height: 40, borderColor: 'gray', borderWidth: 1}}
          value="Search"
        />
      </View>
    )
  }
}

获取此值的python代码是

<div class="form-group">
  <label for="acadYear">Academic Year:</label>
  <select id="acadYear" name"acadYear">
    <option value="2017-18">This Academic Year</option>
    <option value="2018-19">Next Academic Year</option>
  </select>
</div>

这不会返回任何内容。当我尝试

acadYear=self.request.get('acadYear')

抛出错误

acadYear=self.request.get_all('acadYear')

发生了什么?有线索吗?

更多详情

实体

BadValueError: Expected string, got []

接收数据的HTML代码

from google.appengine.ext import ndb

class Allocation(ndb.Model):
  acadYear = ndb.StringProperty()
  branch = ndb.StringProperty()
  semester = ndb.StringProperty()
  subjectCode = ndb.StringProperty()
  subjectName = ndb.StringProperty()
  facultyId = ndb.StringProperty()
  facultyName = ndb.StringProperty()
  choiceNumber = ndb.StringProperty()
  status = ndb.StringProperty()
  createdOn = ndb.DateTimeProperty(auto_now_add=True)

  @classmethod
  def faculty_query(cls, parent_key):
    return cls.query(ancestor=parent_key).order(-cls.createdOn)

Python处理程序

MainHandler类

    <form action="" method="post">
      <legend>Mention Your New Preferences Here</legend>
      <div class="form-group">
        <label for="acadYear">Academic Year:</label>
        <select id="acadYear" name="acadYear">
          <option value="2017-18">This Academic Year</option>
          <option value="2018-19">Next Academic Year</option>
        </select>
      </div>
      <div class="form-group">
        <label for="branch">Branch:</label>
        <input type="text" id="branch" name"branch" value="CSE or ISE or MCA">
      </div>
      <div class="form-group">
        <label for="semester">Semester:</label>
        <input type="text" id="semester" name="semester" value="From 1 to 8"/>
      </div>
      <div class="form-group">
        <label for="choiceNumber">Choice#:</label>
        <input type="text" id="choiceNumber" name="choiceNumber" value="1,2,3"/>
      </div>
      <div class="form-group">
        <label for="subjectCode">Subject Code:</label>
        <input type="text" id="subjectCode" name="subjectCode" value="Example: 10CS43"/>
      </div>
      <div class="form-group">
        <label for="subjectName">Subject Name:</label>
        <input type="text" id="subjectName" name="subjectName" value="Example: Design and Analysis of Algorithms"/>
      </div>
      <div class="form-group">
        <button type="submit">Save Preference</button>
      </div>
    </form>

class MainHandler(webapp2.RequestHandler): def _render_template(self, template_name, context=None): if context is None: context = {} # Get the logged in user user = users.get_current_user() ancestor_key = ndb.Key("User", user.nickname()) qry = Allocation.faculty_query(ancestor_key) context['allocs'] = qry.fetch() template = jinja_env.get_template(template_name) return template.render(context) @ndb.transactional def _create_alloc(self, user): alloc = Allocation(parent=ndb.Key("User", user.nickname()), acadYear=self.request.get_all('acadYear'), branch=self.request.get('branch'), semester=self.request.get('semester'), subjectCode=self.request.get('subjectCode'), subjectName=self.request.get('subjectName'), facultyId=user.user_id(), facultyName=user.nickname(), choiceNumber=self.request.get('choiceNumber'), status='Requested') alloc.put() def get(self): user = users.get_current_user() if user is not None: logout_url = users.create_logout_url(self.request.uri) template_context = { 'user': user.nickname(), 'logout_url': logout_url, } self.response.out.write( self._render_template('main.html', template_context)) else: login_url = users.create_login_url(self.request.uri) self.redirect(login_url) def post(self): user = users.get_current_user() if user is None: self.error(401) self._create_alloc(user) logout_url = users.create_logout_url(self.request.uri) template_context = { 'user': user.nickname(), 'logout_url': logout_url, } self.response.out.write( self._render_template('main.html', template_context)) 返回acadYear=self.request.get_all('acadYear') - 显然位于list - 需要Unicode string进入数据存储。

我试过以下

(1)使用中间变量=不起作用。 (2)编码到putting - 因为UTF-8没有list方法而无法进行编码。 (3)我不知道选择哪个元素encode[0]。因此,我无法尝试 [1]

这么简单 - 但是 - 为什么它必须如此复杂!

感谢任何帮助。我在孩子面前失去了面子。

1 个答案:

答案 0 :(得分:2)

你在这一行有一个错字:

<select id="acadYear" name"acadYear">

您需要添加&#34; =&#34;在&#34; name&#34;之后。由于这个错字,您的浏览器不会发送选择字段的数据。