【问题标题】:price range filter in djangodjango中的价格范围过滤器
【发布时间】:2020-12-21 07:46:37
【问题描述】:

我想按从最低价格到最高价格的价格范围过滤产品。我试过了,但我得到了所有的产品。请向我建议我该怎么做?

    def search(request):
        price_from = request.GET.get('price_from', None)
        price_to = request.GET.get('price_to', None)
        products = Product.objects.filter(price__range=(price_from,price_to))
    
        context = {
            'price_from': price_from,
            'price_to': price_to,
            'products': products,
        }
    
        return render(request, 'search.html', context)

我的模板代码

    <div class="range-slider">
        <form method="get" action="{% url 'search' %}" class="d-flex align-items-center justify-content-between">
            <div class="price-input">
                <label for="amount">PriceFrom </label>
                <input type="number" name="price_from"  value="{{price_from}}">
            </div>
             <div class="price-input">
                <label for="amount">PriceTo</label>
                <input type="number" name="price_to"  value="{{price_to}}">
            </div>
            <button class="filter-btn">filter</button>
        </form>
    </div>

【问题讨论】:

    标签: python django-models django-templates


    【解决方案1】:
    <div class="range-slider">
              <form method="get" action="{% url 'search' %}" class="d-flex align-items-center justify-content-between">
              <div class="price-input">
                <label for="amount">PriceFrom </label>
    #Just Apply For loop for evry price form
                {% for i in price form %}
                <input type="number" name="price_from"  value="{{price_from}}">
                {% end for %}
               </div>
              <div class="price-input">
                <label for="amount">PriceTo</label>
                {% for i in price to %}
                <input type="number" name="price_to"  value="{{price_to}}">
                {% end for %}
              </div>
              <button class="filter-btn">filter</button>
             </form>
              </div>
    </div>
    

    告诉我这是否适合你

    【讨论】:

    • 完成了。我在 view.py 中使用了该格式,'products = Product.objects.filter(price__range=[price_from, price_to])'
    猜你喜欢
    • 2020-12-26
    • 2019-08-03
    • 2016-04-11
    • 1970-01-01
    • 2013-06-16
    • 2016-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多