我正在开发一个允许用户按日期查找的网络应用程序,例如:
results = Something.objects.filter(end = date)
我打算通过 URL 传递日期信息,如下所示:
example.com/invoicer?2/9/1984
然后我将通过 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.
这确实不是生成日期时间的理想方式。你最好使用字符串解析:
>>> datetime.datetime.strptime('2/9/1984', '%m/%d/%Y')
datetime.datetime(1984, 2, 9, 0, 0)
