我有一个由Object预先填充的表单。框架:金字塔,IDE:PyCharm
在页面上,我有额外的字段供用户添加其他电话号码和电子邮件。例如,单个电话对象{tag:work,value:555}将被推入person对象内的phones []数组中。然后我需要将新信息发送回服务器。
的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将抛出“list indices必须为整数,而不是str”错误
有什么想法?不知道这里有什么问题。
self.profiles.update(firstName=first_name, lastName=last_name, organization=organization, title=title, emails=emails, phones=phones)
带有添加字段的表单会在此处创建正确的对象
答案 0 :(得分: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 ["标签"]原始开发人员期待电话的标签而不是我传递的价值。
第二件事,我们的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);
});