def example_2(request): object_list = [{'item': i} for i in xrange(1, 100)] paginator, object_list = paginate(object_list, request) return direct_to_template(request, 'paginator_example_2.html', { 'object_list': object_list, 'paginator': paginator })
def home(request): if not request.user.is_authenticated(): title = "Welcome" context = { "title": title } return render(request, "welcome.html", context) else: # res = teste.delay() # print res title = "Welcome %s" % request.user print(request.user.id) executionList = Execution.objects.filter( request_by__usuario__id=request.user.id).order_by('-id') try: UserProf = UsuarioFriends.objects.get(usuario__id=request.user.id) except: print "Erro. Criando novo userProf" user = User.objects.get(id=request.user.id) UserProf = UsuarioFriends(usuario=user) UserProf.save() print "Criado novo UserProf" paginator = Paginator(executionList, UserProf.resultsPerPage) page = request.GET.get('page') if page is None: page = 1 try: executions = paginator.page(page) except PageNotAnInteger: executions = paginator.page(1) except EmptyPage: executions = paginator.page(paginator.num_pages) # da pra tratar if paginator.count == 0: data = None else: data = executions pageI = paginate(page, paginator) context = { "title": title, "data": data, "pagesIndex": pageI, } return render(request, "home.html", context)
def films(): session = Session(engine) user = current_user if current_user.is_authenticated: user = session.query(Users).get(current_user.id) context = { 'genres': session.query(Genres).all(), 'years': [int(i[0]) for i in session.query(Films.year).distinct()], 'search_request': { 'name': '', 'genres': [], 'year': [], 'country': '', 'length_from': '', 'length_to': '' } } print(request.args.to_dict()) query = session.query(Films) films = query.all() # Set default page number to 1 if page does not exist in request.args page = 1 if request.args: search_request = request.args.to_dict() if 'page' in search_request.keys(): page = int(search_request['page']) del search_request['page'] context['search_request'] = request.args.to_dict() context['search_request']['genres'] = [ int(g) for g in request.args.getlist('genres') ] context['search_request']['year'] = [ int(y) for y in request.args.getlist('year') ] # If filter_query is none, all films will be shown filtered_query = None for key in context['search_request']: if context['search_request'][key]: if key == 'name': filter_spec = [{ 'field': key, 'op': 'ilike', 'value': '%' + context['search_request'][key] + '%' }] print(filter_spec) filtered_query = add_filter(filtered_query, query, filter_spec) elif key == 'genres': for val in context['search_request'][key]: filter_spec = [{ 'field': 'genres', 'op': 'in', 'value': val }] print(filter_spec) if filtered_query is not None: filtered_query = filtered_query.filter( Films.genres_collection.any( Genres.id.in_(context['search_request'][key]))) else: filtered_query = query.filter( Films.genres_collection.any( Genres.id.in_(context['search_request'][key]))) # Check all request args and it to query elif key == 'year': filter_spec = [{ 'field': key, 'op': 'in', 'value': context['search_request'][key] }] print(filter_spec) filtered_query = add_filter(filtered_query, query, filter_spec) elif key == 'country': filter_spec = [{ 'field': key, 'op': 'ilike', 'value': '%' + context['search_request'][key] + '%' }] print(filter_spec) filtered_query = add_filter(filtered_query, query, filter_spec) elif key == 'length_from': filter_spec = [{ 'field': 'length', 'op': '>=', 'value': context['search_request'][key] }] print(filter_spec) filtered_query = add_filter(filtered_query, query, filter_spec) elif key == 'length_to': filter_spec = [{ 'field': 'length', 'op': '<=', 'value': context['search_request'][key] }] print(filter_spec) filtered_query = add_filter(filtered_query, query, filter_spec) if filtered_query is not None: films = filtered_query.all() # Search DEBUG print('films: {}'.format([f.name for f in films])) # Paginate results page_obj = paginate(films, 10, page) return render_template('films/index.html', page_obj=page_obj, context=context, page=page, user=user)
def example_2(request): object_list = [{'item': i} for i in xrange(1, 100)] paginator, object_list = paginate(object_list, request) return direct_to_template(request, 'paginator_example_2.html', {'object_list': object_list, 'paginator': paginator})