[英]If the field is not entered, how to make it not filtered in django
我正在創建一個查找字段,並且想知道如何做到這一點,如果未輸入字段,即使其值為 null,它也不會出現在地址欄中。
http://localhost:8000/pesquisa/?prova=
至
http://localhost:8000/pesquisa/
視圖.py
def index(request):
provas = Prova.objects.all().order_by('idProva')
questoes = Questao.objects.filter().order_by('idProva')
categorias = Categoria.objects.all().order_by('nomeCategoria')
form = QuestaoProvaForm()
return render(request, 'polls/index.html',{'categorias':categorias,'questoes':questoes,'provas':provas,'form':form})
def pesquisa(request):
template_name = 'polls/pesquisa.html'
query = request.GET.get('q', '')
prova = request.GET.get('prova', '')
questao = request.GET.get('questao', '')
categoria = request.GET.get('categoria', '')
results = Questao.objects.filter(idProva=prova)
return render(request, template_name, {'results': results,'prova':prova,'questao':questao,'questoes':questoes})
我不確定是否只有 django 解決方案,但您始終可以使用客戶端 javascript 執行此操作:
var remove_null_values = ($this) => {
if (!$this) {
$this = this;
}
$($this).find('input', 'select').each(function() {
if($(this).val() === '') {
$(this).remove();
}
});
}
$('#formId').submit(function() {
remove_null_values(this);
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.