Dominic
Dominic

Reputation: 117

Best practices to filter in Django

In one of the pages of my Django app I have a page that simply displays all employees information in a table:

Like so:

First Name: Last Name: Age: Hire Date:
Bob         Johnson    21   03/19/2011
Fred        Jackson    50   12/01/1999

Now, I prompt the user for 2 dates and I want to know if an employee was hired between those 2 dates.

For HTTP GET I just render the page and for HTTP POST I'm sending a URL with the variables in the URL.

my urls.py file has these patterns:

('^employees/employees_by_date/$','project.reports.filter_by_date'),
('^employees/employees_by_date/sort/(?P<begin_date>\d+)/(? P<end_date>\d+)/$', EmployeesByDate.as_view()),

And my filter_by_date function looks like this:

def filter_by_date(request):
if request.method == 'GET':
    return render(request,"../templates/reports/employees_by_date.html",{'form':BasicPrompt(),})
else:
    form = BasicPrompt(request.POST)
    if form.is_valid():
        begin_date = form.cleaned_data['begin_date']
        end_date = form.cleaned_data['end_date']
    return HttpResponseRedirect('../reports/employees_by_date/sort/'+str(begin_date)+'/'+str(end_date)+'/')

The code works fine, the problem is I'm new to web dev and this doesn't feel like I'm accomplishing this in the right way. I want to use best practices so can anyone either confirm I am or guide me in the proper way to filter by dates?

Thanks!

Upvotes: 1

Views: 487

Answers (2)

bcattle
bcattle

Reputation: 12819

Use Unix timestamps instead of mm/dd/yyyy dates. A unix timestamp is the number of seconds that have elapsed from Jan 1 1970. ("The Epoch".) So it's just a simple integer number. As I'm writing this, the Unix time is 1432071354.

They aren't very human-readable, but Unix timestamps are unambiguous, concise, and can be filtered for with the simple regex [\d]+.

You'll see lots of APIs around the web use them, for example Facebook. Scroll down to "time based pagination", those numbers are Unix timestamps.

The problem with mm/dd/yyyy dates is ambiguity. Is it mm/dd/yyyy (US)? or dd/mm/yyyy (elsewhere)? What about mm-dd-yyyy?

Upvotes: 1

Antoine Fontaine
Antoine Fontaine

Reputation: 802

You're right, it's a bit awkward to query your API in that way. If you need to add the employee name and something else to the filter, you will end up with a very long URL and it won't be flexible.

Your filter parameters (start and end date) should be added as a query in the url and not be part of path.
In this case, the url would be employees/employees_by_date/?start_date=xxx&end_date=yyy and the dates can be retrieved in the view using start_date = request.GET['start_date].

If a form is used with method='get', the input in the form are automatically converted to a query and appended at the end of the url.
If no form is used, parameters need to be encoded with a function to be able to pass values with special characters like \/ $%.

Upvotes: 2

Related Questions