Break nested loop in Django views.py with a function
Posted
by knuckfubuck
on Stack Overflow
See other posts from Stack Overflow
or by knuckfubuck
Published on 2010-04-27T05:18:13Z
Indexed on
2010/04/27
5:23 UTC
Read the original article
Hit count: 508
I have a nested loop that I would like to break out of. After searching this site it seems the best practice is to put the nested loop into a function and use return to break out of it. Is it acceptable to have functions inside the views.py file that are not a view? What is the best practice for the location of this function? Here's the example code from inside my views.py
@login_required
def save_bookmark(request):
if request.method == 'POST':
form = BookmarkSaveForm(request.POST)
if form.is_valid():
bookmark_list = Bookmark.objects.all()
for bookmark in bookmark_list:
for link in bookmark.link_set.all():
if link.url == form.cleaned_data['url']:
# Do something.
break
else:
# Do something else.
else:
form = BookmarkSaveForm()
return render_to_response('save_bookmark_form.html', {'form': form})
© Stack Overflow or respective owner