[Solved]-Django MultiValueDictKeyError while requesting get object

15πŸ‘

βœ…

On first request there is no form data submitted so request.GET would not have any data. So doing request.GET['pub_date_from'] will fail. You shall use .get() method

pub_from = request.GET.get('pub_date_from')
pub_to = request.GET.get('pub_date_to')

If these keys are not in the dict, will return None. So handle the such cases appropriately in your code.

Also, if you want to filter objects for ListView add get_queryset() method to return filtered queryset as explained here Dynamic filtering

πŸ‘€Rohan

Leave a comment