0

我有一个由对象预先填充的表单。框架:金字塔,IDE:PyCharm

在页面上,我有额外的字段供用户添加额外的电话号码和电子邮件。例如,单个电话对象 {tag:work, value:555} 将被推入 person 对象内部的电话[] 数组中。然后我需要将新信息发送回服务器。

jQuery

// Person
var person = {
    username : profileData.username,
    firstName : "",
    lastName : "",
    phones : [],
    emails : [],
    organization : "",
    title : ""
}

// Original Profile
person.firstName = $('#profile-firstname').val();
person.lastName  = $('#profile-lastname').val();
person.organization = $('#profile-company').val();
person.title  = $('#profile-title').val();

// Added Profile
var phoneObjs   = {};
var emailObjs   = {};
var extraPhones = [];
var extraEmails = [];
var addedPhones = $('.added_phone');
var addedEmails = $('.added_email');

addedPhones.each( function(i) {
    //console.log(i);
    var tag = $(this).children("label").text();
    var value = $(this).children("input").val();
    phoneObjs = $(this).map(function(i,el) {
        var $el = $(el);
        return {
            tag: tag,
            value:value
        };
    }).get();
    person.phones.push(phoneObjs);
    console.log(phoneObjs);
});

Python

def save_profile(self):
    success_msgs = ['Saved! Worry not, nothing sent to the NSA... as far as we know', "Profile Saved, doesn't it feel great to be updated?"]
    error_msgs = ['Internets are clogged up, our monkeys are checking it out', 'Hmm, everything look correct below?', "A BUG! Probably in our code, we're checking it out"]
    try:
        json = self.request.json_body
        first_name = str(json['firstName'])
        last_name = str(json['lastName'])
        organization = str(json['organization'])
        title = str(json['title'])
        phones = (json['phones'])
        emails = (json['emails'])
        self.profiles.update(firstName=first_name, lastName=last_name, organization=organization, title=title, emails=emails, phones=phones)
        #print phones
        value = {'result': 'success', 'message': random.choice(success_msgs)}
    except Exception, err:
        print err
        value = {'result': 'error', 'message': random.choice(error_msgs)}

    #returns a json response
    return self.respond(value)

profile.update

if tools.not_blank(phones):
        lst = phones
        cnt = len(lst)
        current = profile.phones
        for x in xrange(cnt):
            o = lst[x]
            #key = o.get("key", o.get("guid", None))
            lbl = o["label"]
            tag = o.get("tag", None)
            c = [c for c in current if c.label == lbl]
            c = c[0] if len(c) > 0 else None
            if c:
                m, k = self.codex.decode_composite(c.id)
                if tools.not_blank(tag):
                    tag = tag.lower().title()
                    if c.tag != tag:
                        c.tag = tag
                        self.get_query("UPDATE member_phone SET Type = @tag WHERE ID = @id;", {"tag": tag, "id": k}).update()
            else:
                #create a new email address for this member
                self.members.register_phone_number(member=member, telephone=lbl, tag=tag)

    if tools.is_blank(remove):
        return profile


IDE 将在此处抛出“列表索引必须是整数,而不是 str”错误

有什么想法吗?不知道这里有什么问题。

self.profiles.update(firstName=first_name, lastName=last_name, organization=organization, title=title, emails=emails, phones=phones)


添加字段的表单在此处创建正确的对象

在此处输入图像描述 在此处输入图像描述

4

1 回答 1

0

当我深入研究 self.profiles.update() 时,这是我的 jQuery 的问题

if tools.not_blank(phones):
        lst = phones
        cnt = len(lst)
        current = profile.phones
        for x in xrange(cnt):
            o = lst[x]
            #key = o.get("key", o.get("guid", None))
            lbl = o["label"]

第一个问题是 lbl = 0["label"] 原来的开发者期待的是 Phones 的标签,而不是我传递的值。

第二件事,我们的 iOS 开发人员指出我有一个嵌套数组[[]]并没有注意到这一点,所以再次回到 jQuery:

不得不改变:person.phones.push(phoneObjs);person.phones = phoneObjs;

现在它只返回[]电话并且没有列表错误:)

更新了 jQuery

addedPhones.each( function(i) {
    var tag = $(this).children("label").text();
    var value = $(this).children("input").val();
    phoneObjs = $(this).map(function(i,el) {
        var $el = $(el);
        return {
            tag: tag,
            label:value
        };
    }).get();
    person.phones = phoneObjs;
    console.log(phoneObjs);
});
于 2013-10-08T15:36:55.640 回答