I’m doing tests with django on my pc, if I run the code without else the if statement is true and r1 variable is right.
def trovapernome(request, aggregatore, nome):
url = urllib.urlopen("http://127.0.0.1:8000/media/%s.xml" % (aggregatore))
richiesta = url.read()
tree = ET.fromstring(richiesta)
iterator = tree.getiterator("location")
t = get_template('prova.xml')
for name in tree.iter('name'):
if name.text.lower() == nome.lower():
c = Context({'id' : name.text})
r1 = HttpResponse(t.render(c), mimetype='application/xml')
return r1
otherwise this code:
def trovapernome(request, aggregatore, nome):
url = urllib.urlopen("http://127.0.0.1:8000/media/%s.xml" % (aggregatore))
richiesta = url.read()
tree = ET.fromstring(richiesta)
iterator = tree.getiterator("location")
t = get_template('prova.xml')
for name in tree.iter('name'):
if name.text.lower() == nome.lower():
c = Context({'id' : name.text})
r1 = HttpResponse(t.render(c), mimetype='application/xml')
else: r1 = HttpResponse(t.render(Context({'id' : 'prova'})), mimetype='application/xml')
return r1
return always the else statement
You are not looping in the first code block as the
returnisn’t indented correctly.See how the
returnis matched in your second block when usingelse