Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def products(request):
- categories = Category.objects.all()
- if request.method == 'POST':
- pagination_content = ""
- page_number = request.POST['data[page]'] if request.POST['data[page]'] else 1
- page = int(page_number)
- name = request.POST['data[name]']
- sort = '-' if request.POST['data[sort]'] == 'DESC' else ''
- search = request.POST['data[search]']
- max = int(request.POST['data[max]'])
- cur_page = page
- page -= 1
- per_page = max # Set the number of results to display
- start = page * per_page
- # If search keyword is not empty, we include a query for searching
- # the "content" or "name" fields in the database for any matched strings.
- if search:
- all_posts = Product.objects.filter(Q(content__contains = search) | Q(name__contains = search)).exclude(available = False).order_by(sort + name)[start:per_page]
- count = Product.objects.filter(Q(content__contains = search) | Q(name__contains = search)).exclude(available = False).count()
- else:
- all_posts = Product.objects.exclude(available = False).order_by(sort + name)[start:cur_page * max]
- count = Product.objects.exclude(available = False).count()
- if all_posts:
- for post in all_posts:
- pagination_content += '''
- <div class='col-sm-3'>
- <div class='panel panel-default'>
- <div class='panel-heading'>%s</div>
- <div class='panel-body p-0 p-b'>
- <a href='%s'>
- <img src='%s' width='%s' class='img-responsive'>
- </a>
- <div class='list-group m-0'>
- <div class='list-group-item b-0 b-t'>
- <i class='fa fa-calendar-o fa-2x pull-left ml-r'></i>
- <p class='list-group-item-text'>Price</p>
- <h4 class='list-group-item-heading'>$%s</h4>
- </div>
- <div class='list-group-item b-0 b-t'>
- <i class='fa fa-calendar fa-2x pull-left ml-r'></i>
- <p class='list-group-item-text'>On Stock</p>
- <h4 class='list-group-item-heading'>%d</h4>
- </div>
- </div>
- </div>
- <div class='panel-footer'>
- <a href='%s' class='btn btn-primary btn-block'>View Item</a>
- </div>
- </div>
- </div>
- ''' %(post.name, Helpers.get_path('product/' + str(post.id)), Helpers.get_path(post.featured_image), '100%', post.price, post.quantity, Helpers.get_path('product/' + str(post.id)))
- else:
- pagination_content += "<p class='bg-danger p-d'>No results</p>"
- return JsonResponse({
- 'content': pagination_content,
- 'navigation': Helpers.nagivation_list(count, per_page, cur_page),
- })
- else:
- return render(request, Helpers.get_url('product/index.html'), {'categories': categories})
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement