我正在创建一个查找字段,并且想知道如何做到这一点,即使没有输入该字段,即使其值为空,它也不会出现在地址栏中。
http://localhost:8000/pesquisa/?prova=
到
http://localhost:8000/pesquisa/
views.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})
答案 0 :(得分:0)
我不确定是否只有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);
});