python - Break nested loop in Django views.py with a function -


i have nested loop break out of. after searching site seems best practice put nested loop function , use return break out of it. acceptable have functions inside views.py file not view? best practice location of function? here's example code inside 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()             bookmark in bookmark_list:                 link in bookmark.link_set.all():                     if link.url == form.cleaned_data['url']:                         # something.                         break                     else:                         # else.         else:             form = bookmarksaveform()         return render_to_response('save_bookmark_form.html', {'form': form}) 

you shouldn't think of django views being in way special. it's python. such, can have whatever functions in views.py. limitation views have take request object , return subclass of httpresponse. other that, can in module, including having functions, classes or constants used views.

if have lot of utility functions, may want consider extracting them eg lib.py in app directory sake of tidiness. there's no need if you've got 1 or two.


Comments

Popular posts from this blog

javascript - Enclosure Memory Copies -

php - Replacing tags in braces, even nested tags, with regex -