2012-09-19 82 views
1

更新0蟒蛇名单似乎与self.request.get_all改变(

下面是我的模板代码。

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

This question follows from this one

我有这个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 - 

及以下职位代码获取提交按钮从上面的代码 用户后的输出已作出要求下面的代码的变化。

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奇异的是一个单独的列表而不是一个字符串。这导致了错误,我想。

+0

什么是您collisions.html模板做'cases'? –

+1

澄清; '[U“[u'court11730' ]”]'为被一个字符串元件,其恰好是'[u'court11730' 的字符串表示一个列表]'列表。这意味着你将文字列表放入一个表单元素;你不能这样做,并期望它的工作。我们需要看到您的模板代码来帮助您解决这个问题。 –

+0

'注意我用的Jinja2的过滤器'safe'但即使去除并没有改变结果。还什么是有点不同,这里是我的' zerowords

回答

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 %} 

注意<input />元素必须是空的元素,根据(所以使用<../>)到HTML标准。