Don't include blank fields in GET request emitted by Django form -
on django-powered site, have search page several optional fields. search page django form, , view function typical:
def search(request): form = searchform(request.get or none) if form.is_valid(): return form.display_results(request) return render(request, 'search.html', {'form': form})
form.display_results() uses fields provided query db , render response. search.html includes:
<form action="/search/" method="get">{% csrf_token %} <!-- render form fields --> <input type="submit" value="search" /> <input type="reset" value="reset form" /> </form>
since searches have several blank fields, i'd not include them in request emitted submit button on search.html. current searches like:
http://mysite/search/?csrfmiddlewaretoken=blah&optional_field1=&optional_field2=&optional_field3=oohiwantthisone
and i'd them like:
http://mysite/search/?csrfmiddlewaretoken=blah&optional_field3=oohiwantthisone
of course, have several more fields. nice have because make search urls more human-parsable , sharable.
you use jquery button trigger. give form , submit button ids.
$("#button_id").click(function(){ $("input").each(function(){ if($(this).val() == '') { $(this).remove(); } }); $("#form_id").submit(); });
that (or similar) should remove empty fields before submit.
Comments
Post a Comment