def popular(request): qs = Question.objects.popular() page_number = request.GET.get('page', 1) paginator = Paginator(qs, request.GET.get('limit', 10)) paginator.base_url = reverse('popular') + '?page=' return render(request, 'question_list.html', { 'paginator': paginator, 'page': paginator.page(page_number), })
def get_queryset_with_paginator(self, model_or_qset, base_url=None, search_query=None, obj_per_page=OBJECTS_PER_PAGE, manager='objects', result_manager='objects', distinct=False, annotate=None, order_non_fields=None): if hasattr(model_or_qset, '_meta'): model = model_or_qset else: model = model_or_qset.model base_url = base_url or self.request.path queryset = self.get_queryset_ids(model_or_qset, search_query=search_query, manager=manager, distinct=distinct, annotate=annotate, order_non_fields=order_non_fields) paginator = Paginator(queryset, obj_per_page) try: page = paginator.page(self.page) except InvalidPage: raise Http404(_('Page %s does not exist.') % self.page) # Django that can't throw exceptions other than 404. result_qset = getattr( model, result_manager).filter(id__in=tuple(page.object_list)) if annotate: result_qset = result_qset.annotate(**annotate) if self.extra: result_qset = result_qset.extra(**self.extra) if self.order: result_qset = result_qset.order_by(*self.order) # 1082 if order_non_fields: result_qset = result_qset.order_by(order_non_fields) # XXX page.object_list = result_qset paginator.current_page = page paginator.current_object_list = result_qset paginator.base_url = base_url return paginator
def main(request): qs = Question.objects.new() limit = request.GET.get('limit', 10) page_number = request.GET.get('page', 1) paginator = Paginator(qs, limit) paginator.base_url = reverse('main') + '?page=' return render(request, 'question_list.html', { 'paginator': paginator, 'page': paginator.page(page_number) })
def get_queryset_with_paginator( self, model_or_qset, base_url=None, search_query=None, obj_per_page=OBJECTS_PER_PAGE, manager='objects', result_manager='objects', distinct=False, annotate=None, order_non_fields=None): if hasattr(model_or_qset, '_meta'): model = model_or_qset else: model = model_or_qset.model base_url = base_url or self.request.path queryset = self.get_queryset_ids( model_or_qset, search_query=search_query, manager=manager, distinct=distinct, annotate=annotate, order_non_fields=order_non_fields) paginator = Paginator(queryset, obj_per_page) try: page = paginator.page(self.page) except InvalidPage: raise Http404(_('Page %s does not exist.') % self.page) # Django that can't throw exceptions other than 404. result_qset = getattr( model, result_manager).filter(id__in=tuple(page.object_list)) if annotate: result_qset = result_qset.annotate(**annotate) if self.extra: result_qset = result_qset.extra(**self.extra) if self.order: result_qset = result_qset.order_by(*self.order) # 1082 if order_non_fields: result_qset = result_qset.order_by(order_non_fields) # XXX page.object_list = result_qset paginator.current_page = page paginator.current_object_list = result_qset paginator.base_url = base_url return paginator
def popular_view(request, *args, **kwargs): try: page = int(request.GET.get('page', 1)) except: page = 1 questions = Question.objects.all().order_by('-rating') paginator = Paginator(questions, 10) paginator.base_url = '/question/' questions = paginator.page(page) return render(request, '/home/box/web/ask/qa/templates/questions.html', { 'questions':questions, 'paginator':paginator })
def new_questions(request): questions = Question.objects.new() limit = 10 try: page = int(request.GET.get('page', 1)) except ValueError: raise 404 paginator = Paginator(questions, limit) paginator.base_url = reverse('qa:new_questions') + "?page=" try: page = paginator.page(page) except EmptyPage: page = paginator.page(paginator.num_pages) return render(request, 'qa/new_questions.html', { 'questions': page.object_list, 'paginator': paginator, 'page': page })
def page_view(request, *args, **kwargs): try: page = int(request.GET.get('page', 1)) except: page = 1 #questions = [] #for i in range(55): # question = Question(title = 'title' + str(i), text='text1' + str(i), id = i) # questions.append(question) questions = Question.objects.all().order_by('-id') paginator = Paginator(questions, 10) paginator.base_url = '/question/' questions = paginator.page(page) return render(request, '/home/box/web/ask/qa/templates/questions.html', { 'questions':questions, 'paginator':paginator })
def get_queryset_with_paginator(self, models, base_url=None, search_query=None, obj_per_page=OBJECTS_PER_PAGE, managers='objects', distinct=False): base_url = base_url or self.request.path if isinstance(managers, basestring): managers = [managers] * len(models) model_qsets = [] for model, manager in zip(models, managers): model_manager = getattr(model, manager) model_qset = model_manager.all() model_qsets.append(model_qset) if type(search_query) is dict: querysets = [ model_qset.filter(**search_query) for model_qset in model_qsets ] elif type(search_query) is tuple: querysets = [ model_qset.filter(*search_query[0], **search_query[1]) for model_qset in model_qsets ] elif callable(search_query): querysets = [ search_query(model_qset) for model_qset in model_qsets ] elif search_query is not None: try: querysets = [ model_qset.filter(search_query) for model_qset in model_qsets ] except: querysets = [ search_query.values_list('id', flat=True) for model_qset in model_qsets ] else: querysets = model_qsets if self.order: querysets = [ queryset.order_by(*self.order) for queryset in querysets ] if distinct: querysets = [queryset.distinct() for queryset in querysets] paginator = Paginator(MultiQueryset(querysets), obj_per_page) try: page = paginator.page(self.page) except InvalidPage: raise Http404(_('Page %s does not exist.') % self.page) # Django that can't throw exceptions other than 404. # result_qset = getattr( # model, result_manager).filter( # id__in=tuple(page.object_list)) # if self.order: # result_qset = result_qset.order_by(*self.order) # 1082 paginator.current_page = page paginator.current_object_list = page.object_list paginator.base_url = base_url return paginator
def get_queryset_with_paginator( self, models, base_url=None, search_query=None, obj_per_page=OBJECTS_PER_PAGE, managers='objects', distinct=False): base_url = base_url or self.request.path if isinstance(managers, basestring): managers = [managers] * len(models) model_qsets = [] for model, manager in zip(models, managers): model_manager = getattr(model, manager) model_qset = model_manager.all() model_qsets.append(model_qset) if type(search_query) is dict: querysets = [ model_qset.filter(**search_query) for model_qset in model_qsets] elif type(search_query) is tuple: querysets = [ model_qset.filter(*search_query[0], **search_query[1]) for model_qset in model_qsets] elif callable(search_query): querysets = [ search_query(model_qset) for model_qset in model_qsets] elif search_query is not None: try: querysets = [ model_qset.filter(search_query) for model_qset in model_qsets] except: querysets = [ search_query.values_list('id', flat=True) for model_qset in model_qsets] else: querysets = model_qsets if self.order: querysets = [ queryset.order_by(*self.order) for queryset in querysets] if distinct: querysets = [queryset.distinct() for queryset in querysets] paginator = Paginator( MultiQueryset(querysets), obj_per_page) try: page = paginator.page(self.page) except InvalidPage: raise Http404(_('Page %s does not exist.') % self.page) # Django that can't throw exceptions other than 404. # result_qset = getattr( # model, result_manager).filter( # id__in=tuple(page.object_list)) # if self.order: # result_qset = result_qset.order_by(*self.order) # 1082 paginator.current_page = page paginator.current_object_list = page.object_list paginator.base_url = base_url return paginator
def __construct_paginator(self, base_url, question_query_set): paginator = Paginator(question_query_set, per_page=self.ITEMS_PER_PAGE) paginator.base_url = base_url return paginator