1

更新 0

下面是我的模板代码。

There is a potential collision in your reservation at <emph style="font-weight: bold">{{ location_id }}</emph>
<br /><br />
<form action="/unexpected" method="post" >
       <input type="hidden" name="location_id" value="{{ location_id }}"></input>
       <input type="hidden" name="cases" value="{{ cases|safe }}"></input>
    <table border="1" cellpadding="2" 
      cellspacing="0" class="sortable" id="unique-id" align="center">
      <thead> 
<tr> 
<td>Time</td>
<td>Court</td>
<td>Currently signed up</td>
<td>You could sign up this person</td>
</tr></thead>

    <tbody>
    {% for time in times %}
<tr> 
    {% for column in time %}
    <td >
    {{ column|safe }}
    </td>
    {% endfor %}
</tr> 
{% endfor %}
    </tbody>
</table>
<div id="inputdata">

<label>Click the "Submit" button after selecting your option(s) above. Another option is to press the browser back button and then refresh/reload the page to see the current status without taking action here.
</label>
<input type="submit" value="submit" />
</div>
</form>

更新 0

这个问题来自这个问题

我有这个def post()代码。

    cases=[]
    for res in tempres:
        r = TempReservations.get_or_insert(time[2],parent=res[8], day = int(res[9]))
        r.hour = res[0][0]
        r.minute = res[0][1]
        r.time = res[0][2]
        r.name = res[4]
        r.hiddenname = res[5]
        r.court_id = res[6]
        r.weekday = res[7]
        r.weekday_key = str(res[8])
        r.put()

        cases.append(r.court_id+str(r.hour)+str(r.minute))
        tt=r.court_id+str(r.hour)+str(r.minute)
        logging.info("string: %s" % tt)
        logging.info("cases: %s" % cases)
        for case in cases:
            logging.info("case: %s" % case)
    template_values = {'times':times,'cases':cases,'location_id':location_id}
    path = os.path.join(TEMPLATE_DIR, 'collisions.html')
    self.response.out.write(template.render(path, template_values))

这会在日志中产生以下内容。

INFO     2012-09-19 21:46:43,064 views.py:207] string: court11730
INFO     2012-09-19 21:46:43,064 views.py:208] cases: [u'court11730']
INFO     2012-09-19 21:46:43,064 views.py:210] case: court11730
INFO     2012-09-19 21:46:43,080 dev_appserver.py:2967] "POST /read/Rogers HTTP/1.1" 200 -

在用户进行了需要下面代码的更改后,下面的 post 代码从前面的代码中获取提交按钮的输出。

def post(self):
    location_id=self.request.get('location_id')
    cases=self.request.get_all('cases')
    for case in cases:
        logging.info("cases: %s " % cases)
        logging.info("case: %s " % case)
        logging.info("kcase: %s " % 'k'+case)
        key=str(self.request.get('k'+case))
        logging.info("key: %s " % key)

日志为此代码生成了以下内容。

INFO     2012-09-19 21:47:47,320 views.py:678] cases: [u"[u'court11730']"] 
INFO     2012-09-19 21:47:47,320 views.py:679] case: [u'court11730'] 
INFO     2012-09-19 21:47:47,320 views.py:680] kcase: k [u'court11730']
INFO     2012-09-19 21:47:47,320 views.py:682] key:  
ERROR    2012-09-19 21:47:47,321 webapp2.py:1553] Invalid string key .
  File "/Users/brian/googleapps/scheduler/views.py", line 684, in post
    res = Reservations.get(key)

注意上面的第 208 行和上面的第 678 行是如何cases变化的,现在似乎是列表中的列表,而不仅仅是列表,因此现在case单数是单个列表而不是字符串。我认为这会导致错误。

4

1 回答 1

1

要将列表作为隐藏元素传递到表单中,您需要为<input type="hidden"/>列表中的每个项目创建一个新元素:

<form action="/unexpected" method="post" >
<input type="hidden" name="location_id" value="{{ location_id }}"/>
{% for item in cases %}
  <input type="hidden" name="cases" value="{{ item }}" />
{% endfor %}

请注意,根据 HTML 标准,<input />元素必须是空元素(因此使用)。<../>

于 2012-09-19T22:35:02.907 回答