'Set default url parameter or ignore the none input when nothing is specified - Django Project

I have a list of dictionaries:

mylist=
[{'Date': '10/2/2021', 'ID': 11773, 'Receiver': 'Mike'},
{'Date': '10/2/2021', 'ID': 15673, 'Receiver': 'Jane'},
{'Date': '10/3/2021', 'ID': 11773, 'Receiver': 'Mike'},
... 
{'Date': '12/25/2021', 'ID': 34653, 'Receiver': 'Jack'}]

I want to select the rows based on my input date range. I've successfully done this by appending the URL with ?start=val1&end=val2 and I was able to call the input date values from views.py to template.html :

Appending the URL with ?start=val1&end=val2 based on my input dates in my HTML

<div class="d-sm-flex align-items-center justify-content-between mb-4">
  <form method="get" action="/chart/">
    <div class="form-row">
     <label for="start">Start Date:</label>
     <div class="col">
        <input type="date" class="form-control" name="start"  min="2020-01-03" required>
  </div>
  <label for="end">End Date:</label>
  <div class="col">
      <input type="date" class="form-control" name="end"  min="2020-01-03" required>
  </div>
  <button type="submit" class="btn btn-primary"> Generate Report</button>
</div>
</form>                   

Define start and end in my views.py:

start = request.GET.get('start', None)
end = request.GET.get('end', None)

Select the rows based on the input date range in my views.py:

dfmylist = pd.DataFrame(mylist)
dfmylist['Date'] = pd.to_datetime(dfmylist['Date']) # you missed this line
dfmylistnew = (dfmylist['Date'] > start) & (dfmylist['Date'] <= end)
new = dfmylist.loc[dfmylistnew]

And I was able to recall the {{new}} in my HTML. The only thing is, when I open the default URL, it shows the error: `Invalid comparison between dtype=datetime64[ns] and NoneType'. I know this is because no date values have been input, so the start & end will have no value.

What should I add in my views.py, so that the default URL without selected dates will assign a default value for start and end?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source