def _sort(request): sort_form = SortForm(request.GET) if sort_form.is_valid() and sort_form.cleaned_data['sort']: order = str(sort_form.cleaned_data['sort']) else: order = '-id' return Decision.objects.order_by(order)
def all(): goals = Goal.query.all() form = SortForm() if request.method == "POST": if request.form["sort_order"] == "best": teachers = Teacher.query.order_by(Teacher.rating.desc()).all() elif request.form["sort_order"] == "expensive": teachers = Teacher.query.order_by(Teacher.price.desc()).all() elif request.form["sort_order"] == "cheap": teachers = Teacher.query.order_by(Teacher.price).all() else: teachers = Teacher.query.order_by(func.random()).all() return render_template('all.html', goals=goals, teachers=teachers, form=form) # Заполненная форма выбора не передана else: teachers = Teacher.query.order_by(func.random()).all() return render_template('all.html', goals=goals, teachers=teachers, form=form)
def product_list(url): collection = Submenu.query.filter(Submenu.url == url).first_or_404() products = Product.query.filter(Product.collection == collection.id) category = MainMenu.query.filter( MainMenu.id == collection.main_menu_id).first() page = request.args.get('page') if page and page.isdigit(): page = int(page) else: page = 1 form = SortForm() if form.validate_on_submit(): if form.min_price.data: products = products.filter( Product.price >= int(form.min_price.data)) if form.max_price.data: products = products.filter( Product.price <= int(form.max_price.data)) if form.exist.data: products = products.filter(Product.availability == 'В наличие') if form.sort.data == 'price': products = products.order_by(Product.price) if form.sort.data == '-price': products = products.order_by(Product.price.desc()) pages = products.paginate(page=page, per_page=1) print(request.query_string) st = str(request.query_string) return render_template('products_list.html', products=products, url=url, collection=collection, category=category, form=form, pages=pages, st=st)
def listing(request, status): extra_context = context_list[status] extra_context['sort_form'] = SortForm(request.GET) status_code = context_codes[status] queryset = _filter(_sort(request), status_code) return list_detail.object_list(request, queryset, template_name='consensus_list.html', extra_context=extra_context)
def stored_requests(request): form = SortForm(request.GET or None) order = form.order_by() if form.is_valid() else 'date_added' latest = Request.objects.order_by(order)[:10] return render(request, 'stored_requests.html', {'stored_requests': latest, 'form': form})