def month(request, year, month): try: year = int(year) month = int(month) except (ValueError): raise Http404() try: archive = format(datetime(year, month, 1), 'b,Y') except (ValueError): raise Http404() entries = Entry.objects.filter(pub_date__year=year, pub_date__month=month, status__exact='publish') if not entries: Http404() entries = Paginator(entries.order_by('-pub_date'), PAGE_SIZE) page_id = get_page_id(request.GET.get('page', '1')) current_page = page(entries, page_id) data = { 'name': 'Archive', 'archive': archive, 'entries': current_page.object_list, 'has_next': current_page.has_next(), 'next': page_id + 1, 'has_previous': current_page.has_previous(), 'previous': page_id - 1 } return data
def month(request,year,month): try: year = int(year) month = int(month) except(ValueError): raise Http404() try: archive = format(datetime(year,month,1),'b,Y') except(ValueError): raise Http404() entries = Entry.objects.filter(pub_date__year = year,pub_date__month = month,status__exact = 'publish') if not entries: Http404() entries = Paginator(entries.order_by('-pub_date'),PAGE_SIZE) page_id = get_page_id(request.GET.get('page','1')) current_page = page(entries,page_id) data = { 'name': 'Archive', 'archive': archive, 'entries': current_page.object_list, 'has_next': current_page.has_next(), 'next': page_id + 1, 'has_previous': current_page.has_previous(), 'previous': page_id - 1 } return data
def articles_list_invini(request): data_arlo = {} data_arlo['arlo'] = {} orderBy = request.GET.get('orderBy') cii = request.GET.get('cii') invini = Invinideta.objects.using(request.db).filter(cii = cii) if request.GET.get('buscarPor'): invini = invini.filter(carlos__nlargo__icontains = request.GET.get('buscarPor')) else: invini invini = Paginator(invini.order_by(orderBy), 10) page = request.GET.get('page') invini = invini.page(page) if len(invini.object_list) < 10: data_arlo['response'] = 0 else: data_arlo['response'] = 1 for queryset in invini: data_arlo['arlo'][queryset.carlos.pk] = { 'carlos': queryset.carlos.pk, 'nlargo': queryset.nlargo, 'cbarras': queryset.carlos.cbarras, 'canti': str(queryset.canti).replace(",", "."), 'vcosto': str(queryset.vunita).replace(",", "."), 'cesdo': queryset.carlos.cesdo.nesdo, 'cmarca': queryset.carlos.cmarca.nmarca, 'cancalcu': str(queryset.cancalcu).replace(",", "."), 'cgrupo': queryset.carlos.cgpo.ngpo } return HttpResponse(json.dumps(data_arlo), content_type="application/json")
def paginatoooor(request, q, **kwargs): """封装django自带的分页组件""" page_info = get_page_info(request) order_field = page_info['order_field'] order_direction = page_info['order_direction'] page_num = page_info['page_num'] page_size = page_info['page_size'] pag = Paginator(q, page_size) try: records = pag.page(page_num) except PageNotAnInteger: # If page is not an integer, deliver first page. records = pag.page(1) except EmptyPage: # If page is out of range (e.g. 9999), deliver last page of results. records = pag.page(pag.num_pages) # # 排序 if order_field: order_fields = [] if isinstance(order_field, (str, unicode)): if order_direction == 'desc': order_fields.append('-%s' % order_field) else: order_fields.append(order_field) elif isinstance(order_field, list): for f in order_field: if order_direction == 'desc': order_fields.append('-%s' % f) else: order_fields.append(f) if order_fields: try: pag = pag.order_by(*order_fields) except Exception: pass ''' try: records = records.all()[offset:offset + page_size] except Exception,e: records = records[offset:offset + page_size] ''' paged = create_success_dict(data={ 'page': page_num, 'page_size': page_size, 'record_count': pag.count, 'page_count': pag.num_pages, 'records': model_list_to_dict(records) }) paged['data'].update(kwargs) return paged
def tag(request,tag): entries = Entry.objects.filter(status__exact='publish',tags__slug = tag) try: tag = entries[0].tags.get(slug = tag) except(IndexError): raise Http404() entries = Paginator(entries.order_by('-pub_date'),PAGE_SIZE) page_id = get_page_id(request.GET.get('page','1')) current_page = page(entries,page_id) data = { 'name': 'Tag', 'tag': tag, 'entries': current_page.object_list, 'has_next': current_page.has_next(), 'next': page_id + 1, 'has_previous': current_page.has_previous(), 'previous': page_id - 1 } return data
def announcements_drives(request): user = request.user drives = user.profile.follows.all() page = request.GET.get('page') # create an empty queryset q = Notifications.objects.none() # union multiple queries together for drive in drives: q = q.union(drive.notifications_set.all()) q = Paginator(q.order_by('-date_posted'), 5) context = { 'data': q.get_page(page), } return render(request, 'users/announcements_drives.html', context=context)
def tag(request, tag): entries = Entry.objects.filter(status__exact='publish', tags__slug=tag) try: tag = entries[0].tags.get(slug=tag) except (IndexError): raise Http404() entries = Paginator(entries.order_by('-pub_date'), PAGE_SIZE) page_id = get_page_id(request.GET.get('page', '1')) current_page = page(entries, page_id) data = { 'name': 'Tag', 'tag': tag, 'entries': current_page.object_list, 'has_next': current_page.has_next(), 'next': page_id + 1, 'has_previous': current_page.has_previous(), 'previous': page_id - 1 } return data
def category(request,cat): if cat == 'uncategories': entries = Entry.objects.filter(status__exact='publish',category__isnull = True) else: entries = Entry.objects.filter(status__exact='publish',category__slug = cat) try: category = entries[0].category except(IndexError): raise Http404() entries = Paginator(entries.order_by('-pub_date'),PAGE_SIZE) page_id = get_page_id(request.GET.get('page','1')) current_page = page(entries,page_id) data = { 'name': 'Category', 'category': category, 'entries': current_page.object_list, 'has_next': current_page.has_next(), 'next': page_id + 1, 'has_previous': current_page.has_previous(), 'previous': page_id - 1 } return data
def get(self, request, **kwargs): pagination = kwargs.get('pagination', Question.objects.all()) pagination_form = kwargs.get( 'pagination_form', QuestionsPaginationForm(request.GET or None)) if pagination_form.is_valid(): pagination = Paginator( pagination.order_by(pagination_form.cleaned_data['order']), pagination_form.cleaned_data['limit']) page = pagination_form.cleaned_data['page'] else: pagination = Paginator(pagination, pagination_form.fields['limit'].initial) page = pagination_form.fields['page'].initial try: pagination = pagination.page(page) except PageNotAnInteger: return HttpResponseNotFound() except EmptyPage: pagination = pagination.page(pagination.num_pages) kwargs['pagination'] = pagination kwargs['pagination_form'] = pagination_form return super().get(request, **kwargs)
def category(request, cat): if cat == 'uncategories': entries = Entry.objects.filter(status__exact='publish', category__isnull=True) else: entries = Entry.objects.filter(status__exact='publish', category__slug=cat) try: category = entries[0].category except (IndexError): raise Http404() entries = Paginator(entries.order_by('-pub_date'), PAGE_SIZE) page_id = get_page_id(request.GET.get('page', '1')) current_page = page(entries, page_id) data = { 'name': 'Category', 'category': category, 'entries': current_page.object_list, 'has_next': current_page.has_next(), 'next': page_id + 1, 'has_previous': current_page.has_previous(), 'previous': page_id - 1 } return data