def images_view(request, id=None): context = pre_populate_context_dict() if not id: return render(request, 'images/view.html', context) image = File.objects.get(pk=id) context['image'] = image return render(request, 'images/view.html', context)
def images_index(request): context = pre_populate_context_dict() query = __store_session_query(request, File.get_all_images()) try: del request.session['random_list'] except KeyError: pass try: del request.session['id_list'] except KeyError: pass per_page = 24 paginator = Paginator(query, per_page) page = request.GET.get('page') try: files = paginator.page(page) except PageNotAnInteger: files = paginator.page(1) page = 1 except EmptyPage: files = paginator.page(paginator.num_pages) page = paginator.num_pages page = int(page) page_range = [] threshold = 10 threshold_size = (threshold*2) + 1 # build initial page range list for i in range(1, paginator.num_pages+1): if i+threshold >= page and i <= page: page_range.append(i) elif i-threshold <= page and i >= page: page_range.append(i) # if the length of page_range is less than the threshold size, add to it if len(page_range) < threshold_size: if page <= threshold: i = page_range[-1] while len(page_range) < threshold_size and i < paginator.num_pages: i = i + 1 page_range.append(i) else: i = page_range[0] while len(page_range) < threshold_size and i > 1: i = i - 1 page_range.insert(0, i) if page_range[0] != 1: page_range.insert(0, '...') if page_range[-1] != paginator.num_pages: page_range.append('...') i = per_page * (page-1) + 1 for f in files: f.num = i i = i + 1 context['files'] = files context['page_range'] = page_range if request.GET.get('view') == 'list': context['view'] = 'list' return render(request, 'images/index.html', context)
def home(request): context = pre_populate_context_dict() return render(request, 'home.html', context)