如何在刷新页面后保留选中的选项
how to keep selected option after refresh the page
我正在尝试使用 django 实现过滤器选项我可以进行过滤,但在刷新后无法保留所选选项,因为我正在渲染过滤后的结果,有什么解决方案吗?
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>
Django 管理过滤器使用 GET 请求。这解决了您的问题:
title = request.GET.get('da',None)
<form action="{% url 'search:query' %}" method="get" enctype="multipart/form-data">
解决方案是使用 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>
我正在尝试使用 django 实现过滤器选项我可以进行过滤,但在刷新后无法保留所选选项,因为我正在渲染过滤后的结果,有什么解决方案吗?
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>
Django 管理过滤器使用 GET 请求。这解决了您的问题:
title = request.GET.get('da',None)
<form action="{% url 'search:query' %}" method="get" enctype="multipart/form-data">
解决方案是使用 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>