как сохранить выбранный вариант после refre sh страницы - PullRequest
2 голосов
/ 06 марта 2020

Я пытаюсь реализовать опцию фильтра с django Я могу выполнить фильтрацию, но не могу сохранить выбранную опцию после refre sh, потому что я рендерил отфильтрованный результат, какое-либо решение?

views.py

def filter(request):
    products = Product.objects.all()
    print(products)
    if request.method == 'POST':
      title = request.POST.get('da',None)
      print(title)
      titre = Product.objects.filter(title=title)
      print(titre)
      return render(request, 'searchapp/searchview.html', {'qs': titre,'ds':products})



    return render(request,'searchapp/searchview.html',{'qs':products})

html:

<div>
    <form action="{% url 'search:query' %}" method="post" enctype="multipart/form-data">
      {% csrf_token %}
            <div class="form-group">
                <label>Title</label>
                  <select name="da" class="form-control">
                        {% for obj in qs %}
                 <option value="{{obj}}">{{obj}}</option>
                         {%endfor%}

                         </select>
            </div>
            <button type="submit" class="btn btn-warning btn-lg">Add Product</button>
        </form>
</div>

Ответы [ 2 ]

1 голос
/ 06 марта 2020

решение было использовать GET:

views.py

def filter(request):
      products = Product.objects.all()
      print(products)
      title = request.GET.get('da',None)
      print(title)
      titre = Product.objects.filter(title=title)
      print(titre)
      return render(request, 'searchapp/searchview.html', {'qs': titre,'ds':products})



      return render(request,'searchapp/searchview.html',{'qs':products})

html .py

   <form action="{% url 'search:query' %}" method="GET" enctype="multipart/form-data">
      {% csrf_token %}
            <div class="form-group">
                <label>Title</label>
                  <select name="da" id="da" class="form-control">
                        {% for obj in ds %}
                 <option value="{{obj}}">{{obj}}</option>
                         {%endfor%}
<!--                <input type="text" class="form-control" name="title">-->
                         </select>
            </div>
            <button type="submit" class="btn btn-warning btn-lg">Add Product</button>
        </form>
</div>

1 голос
/ 06 марта 2020

Django Фильтры администратора Используйте запрос GET. Это решит вашу проблему:

title = request.GET.get('da',None)
<form action="{% url 'search:query' %}" method="get" enctype="multipart/form-data">
...