Break nested loop in Django views.py with a function
- by knuckfubuck
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})