def search(request): """Returns search results for the given query.""" if not 'q' in request.GET: return render_to_response('errors.html', { 'errors': ["Invalid query"] }) query = request.GET['q'] if len(query) < 3: return render_to_response('errors.html', { 'errors': ["Queries must be three characters or more"] }) photos = Photo.search(query) photos = sorted(photos, reverse=True, key=lambda p: p.dt_submitted) return render_to_response('search.html', { 'query': query, 'photos': photos })