1

Web.py フレームワークを使用しています。HTML ページに動的ドロップダウンがあり、jquery と json を使用して正常に動作しています。しかし、複数の属性を持つ選択タグを追加すると、web.py でキーエラーが発生します。どうすればよいですか?この問題を回避してください。

編集: python s = web.input()['text'] KeyError: 'text'で次のエラーが発生します

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)
4

1 に答える 1

1

問題は JavaScript/AJAX 側にあるようです。web.py コードは常に同じことを行い、バグを引き起こす可能性のあるものはまったくないようです。

Firebug または 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)
于 2013-10-01T13:28:15.103 に答える