2013-10-01 41 views
1

我正在使用Web.py framework.I有一个动态的下拉列表,我的html页面工作正常使用jquery和json.But当我添加选择标记与多个属性时,我收到一个在web.py.How中可以避免这个问题。web.py Json与多个选择

编辑:我收到以下错误蟒蛇 S = web.input() '文本'] KeyError异常: '文本'

PS:我在web开发

新手

这是我的JSON/jQuery代码:

<script type="text/javascript" > 

jQuery(document).ready(function() { 
    jQuery("#primaryl").bind('change click', function() { 
     var pid = $$(this).val(); 
     if (pid != '') { 
      jQuery.ajax({ 
       type: "PUT", 
       url: "/getloc", 
       async: false, 
       data: {text: pid}, 
       dataType: "json", 
       success: function(regions) { 
        $$("#secl").empty(); 
        $$("#secl").append("<option value='0'>SECONDARY</option>"); 
        $$.each(regions, function(index, region) { $$("#secl").append("<option>" + region + "</option>"); }); 
       } 
      }); 
     } else { 
      jQuery("#secl").html("Failed"); 
     } 
     return false; 
    }); 
}); 

HTML代码:

<!--first select--> 
<select name="primaryl" id="primaryl" multiple="multiple"> 
    <option value="0">PRIMARY</option> 
</select> 
<!--second select--> 
<select name="secl" id="secl"><option value="0">SECONDARY</option></select> 

web.py代码:

class Getloc: 
    def PUT(self): 
     s = web.input()['text'] 
     result = db.select('location') 
     for user in result: 
      if user.lname == s: 
       lid = user.lid 
     result = db.select('location') 
     sec_dict = [] 
     i = 0 
     for users in (result): 
      if users.lparent==lid: 
       sec_dict.append(users.lname.encode('ascii','ignore')) 
       i = i + 1; 
     if i == 0: 
      sec_dict = ['None'] 
     return json.dumps(sec_dict) 
+0

请发表您的错误回溯。 –

+0

回溯(最近一次通话最后): 文件“C:\ Users \ Admin \ Desktop \ ******”,第170行,PUT s = web.input()['text'] KeyError: 'text' – Rishin

回答

1

才像问题上的JavaScript/AJAX的一面。 web.py代码总是做同样的事情,似乎没有任何可能导致任何错误的东西。

最好的办法是检查与萤火虫或Chrome或Safari的内置的开发/调试控制台传出HTTP请求,看是否text参数是在两种情况下确实存在。

而且,这里是你的Python代码更健全的版本注释:

import json 

import db 
import web 


class Getloc(object): 
    def PUT(self): 
     s = web.input()['text'] 
     # doesn't the DB layer web.py allow you to directly query the rows 
     # that match your criteria? filtering in your code is inefficient 
     for user in db.select('location'): 
      if user.lname == s: 
       lid = user.lid 
       break # once found, save CPU time and don't keep iterating 

     # sec_dict = [] # this is not a dict, it's a list, but it's not 
         # needed anyway--use list comprehensions instead 

     # i = 0 # not needed in this case, but if you need iteration with 
       # indexing, use `for ix, elem in enumerate(elems)` 

     # same question--can't you just have the DB do the filtering? 
     ret = [user.lname.encode('ascii', 'ignore') 
       for user in db.select('location') 
       if user.lparent == lid] 

     # if not ret: 
     #  ret = [None] # this is a bad idea; just return an empty list 

     return json.dumps(ret) 
+0

虽然我没有得到真正的问题的解决方案。只是发现我在python/web.py中有多糟糕:)。谢谢你的评论btw。 – Rishin

+0

然后随便上传:P –

+0

使用正常的select:text:abcd和With multiple select:** text []:abcd **。那么如何将jquery/json中的数组传递给后端。 – Rishin