views:

53

answers:

4

I'm working on a web app that will allow the user to do a lookup by date, so that, for example:

results = Something.objects.filter(end = date)

I'm planning on passing in the date information via the URL, like this:

example.com/invoicer?2/9/1984

I'll then grab the date via request.GET, break off the first part and store it as the month, delete the slash, break off the second part as the date, etc. etc.

I'm not too worried about error/input checking, since only trusted administrators will have access to this, but it seems like a crappy way of going about generating the datetime.

Any better ideas?

+4  A: 

[...] break off the first part and store it as the month, delete the slash, break off the second part as the date, etc. etc.

That's indeed is not an ideal way to generate the datetime. You're better off with string parsing:

>>> datetime.datetime.strptime('2/9/1984', '%m/%d/%Y')
datetime.datetime(1984, 2, 9, 0, 0)
SilentGhost
Gorgeous. Haven't been able to check it left, but exactly what I was hoping for.
Michael Morisy
+1  A: 

You'll have an easier time if you use a parameter for the value:

invoicer?date=2/9/1984

and you might prefer to use an ISO8601 date:

invoicer?date=19840209

I'm not sure what your user interface to this is, or are you expecting people to type these URLs by hand? If not, IS08601 is the way to go.

Ned Batchelder
+2  A: 

Seems pretty normal. One other way we do it is not to use the query string but the path:

example.com/invoicer/end_date/1984-02-09
godswearhats
+2  A: 

you can convert to a unix/epoch date and pass as a long integer. super simple and no worrying about string parsing. just use python's time.gmtime to get your time from the number of seconds.

example.com/invoicer?date=1280139973

time.gmtime(1280139973)
Jason w
Nice too because it obscures the data being sent a little. I don't like sending obvious GET data that users can directly tamper with because it makes the input validation more difficult.
joel3000