2013-10-08 177 views
0

我有一個被對象預先填充的表單。框架:金字塔,IDE:PyCharm錯誤:列表索引必須是整數,而不是str

在頁面上我有額外的字段供用戶添加額外的電話號碼和電子郵件。例如,個人電話對象{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將拋出這裏 「列表索引必須是整數,而不是STR」 錯誤

有什麼想法?不知道這裏有什麼問題。

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


的形式添加的字段創建這裏

enter image description here enter image description here

+0

你在說什麼IDE 關於?告訴我們這是什麼框架,或者至少是'self.profiles'是什麼也可能是有幫助的。 – geoffspear

+0

您能否發佈追蹤信息?你在'self.profiles.update()'方法中做了什麼? –

+0

對不起,它是PyCharm,在金字塔框架中,將profiles.update添加到原始代碼中......我實際上越來越接近了,我的問題是我傳入一個數組中的嵌套數組,並導致問題我相信 –

回答

0

正確的對象這是我的jQuery的一個問題,當我跳入self.profiles。更新()

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); 
}); 
相關問題