【问题标题】:web.py Json with Multiple selectweb.py Json 多选
【发布时间】:2013-10-01 13:16:41
【问题描述】:

我正在使用 Web.py 框架。我的 html 页面中有一个动态下拉列表,使用 jquery 和 json 可以正常工作。但是当我添加具有多个属性的选择标记时,我在 web.py 中收到一个关键错误。我怎样才能避免这个问题。

EDIT:我在 python 中收到以下错误 s = web.input()['text'] KeyError:'文本'

P.S:我是网络开发新手

这是我的 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)

【问题讨论】:

  • 请发布您的错误回溯。
  • Traceback(最近一次调用最后一次):文件“C:\Users\Admin\Desktop******”,第 170 行,在 PUT s = web.input()['text' ] KeyError:'文本'

标签: jquery python html json


【解决方案1】:

看起来问题确实出在 JavaScript/AJAX 方面。 web.py 代码总是做同样的事情,而且似乎根本没有任何可能导致任何错误的东西。

最好的办法是使用 Firebug 或 Chrome 或 Safari 的内置开发/调试控制台检查传出的 HTTP 请求,看看text 参数是否真的在这两种情况下都存在。

另外,这里有一个更健全的 Python 代码版本,带有 cmets:

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)

【讨论】:

  • 虽然我没有得到真正问题的解决方案。刚刚发现我在 python/web.py 中的表现有多么糟糕 :) 。非常感谢你的评论。
  • 使用普通的 select:text:abcd 和多选:text[ ]:abcd。那么如何将 jquery/json 中的数组传递给后端。
  • 那么我就试试input = web.input(); s = input.get('text', input['text[]'])
  • 非常感谢!!它工作得很好!我似乎没有足够的声誉给你一个赞成票!无论如何,再次非常感谢:)
  • 接受就好了:P(实际上我不知道不可能像这样投票)
猜你喜欢
  • 1970-01-01
  • 2014-11-07
  • 2012-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多