mercredi 22 mars 2017

for loop not working on django

I'm sure I'm doing something very stupid wrong, but I can't really figure out what. This same identical code on my django idle works perfectly, but in django it just doesn't.

I have a property_id which is for example 72227686, I want to loop through a dictionary to find which site is that:

site_dic = {'Eight AM': ['84872224','http://eight.org'], 
            'MaltaSky': ['72822237', 'http://Malta.com'], 
            'Rome Tour': ['72227686', 'http://rometour.org']}

print (site_dic) # This return the dictionary above
print (type(site_dic)) # This returns <class 'dict'> (Just to check)
for site,val in site_dic.items():
    print ('trying: ' +val[0]+' and '+property_id)
    if val[0] == property_id: # I know this will happen, so an "else" is almost unnecessary
        property_name = site
        property_url = val[1]
        property_id = val[0]
        print('Matched: '+property_name)
        current_user = str(request.user) # Otherwise some LazyLoad bug comes up! 0.o
        gp_object, created = GoogleProperty.objects.get_or_create(google_email=current_user)
        gp_object.property_name = property_name
        gp_object.property_url = property_url
        gp_object.property_id = property_id
        gp_object.save()
        print('Now the property should be saved!')

     else:
        print('x') # This is impossible

If I run this code on my notebook I get the expected result:

trying: 84872224 and 72227686
trying: 72822237 and 72227686
trying: 72227686 and 72227686
Match found: Rome Tour

However django runs only the first: trying: 84872224 and 72227686 and then prints x and goes on with the code.

Why this happens? Should I use something like continue (however I thought that's more for while loops no?

Aucun commentaire:

Enregistrer un commentaire