1

I have implemented a pretty standard use of Django-filter, and I noticed that when I submit my query, the URL populates not just with the filters I entered, but with blank values for every filter I did not as well.

For example, when I fill out just the STATUS_CODE filter, and then search on my site, I get

http://localhost:8888/request-log/?status_code=200&route=&time_sent_0=&time_sent_1=&python_error_msg=&s3_upload_failed=&s3_error_msg=

Ideally, I would like the URL to only show fields that aren't blank. So, it would go to

http://localhost:8888/request-log/?status_code=200

What is the best way to do this?

Sam Bobel
  • 1,784
  • 1
  • 15
  • 26
  • 2
    Since you haven't included any code, I'm guessing you're using a GET form. And the url is populated as soon as you submit your form button. You will have to intercept the button and write javascript to send request to your server (This is where you'll remove the unnecessary GET parameters). – anupsabraham Oct 19 '17 at 18:39
  • I posted an option for Python/Django in combination with the django-filters app and multiple filters used on the same page. [https://stackoverflow.com/a/68211134/15485550](https://stackoverflow.com/a/68211134/15485550) – linxOD Jul 01 '21 at 13:52

1 Answers1

1

Just to expand on what @anupsabraham said, this is a general question about HTML form use, and isn't specific to django-filter.

One option is to use method="POST" instead of method="GET" in the form, which will submit the data as part of the request body instead of as a querystring.

Alternatively, you could use javascript to intercept the form's submit event, and filter empty inputs. An example of this: jquery: how to remove blank fields from a form before submitting?

Sherpa
  • 1,948
  • 13
  • 25