在 Django 中获取 urlconf 中的日期时间或日期的最佳方法是什么
在我的 URLconf 中,我希望能够完全不传递任何时间、日期或日期+时间,如下所示:
/posts/
/posts/2010-01-01
/posts/2010-01-01 20:30
在我的view 我想创建一个 datetime.datetime 对象或一个 datetime.date 对象
在 urlconf 中编写此对象的最佳方法是什么?
In my URLconf I would like to be able to pass no time at all, a date or a date + time like so:
/posts/
/posts/2010-01-01
/posts/2010-01-01 20:30
In my view I would like to create either a datetime.datetime object or a datetime.date object
What would be the best way of writing this in a urlconf?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议还用斜杠分隔年/月/日,这样可以更轻松地集成(也许稍后也可以)视图,例如。显示年度/每月概览:
I'd recommend to also seperate the year/month/day with slashes, so can easier integrate (maybe also later on) views that eg. display a yearly/monthly overview:
请参阅 http://docs.djangoproject.com/en/dev /topics/http/urls/#named-groups 。可能您需要类似的东西:
注意这是未经测试的,您仍然需要在视图中将其设为日期时间对象,要转换为日期时间,请参阅 http://docs.python.org/library/datetime.html#datetime.datetime
See http://docs.djangoproject.com/en/dev/topics/http/urls/#named-groups . Likely you'll need something like :
Note this is untested, and you'll still have to make this a datetime object in the view, for converting to datetime see http://docs.python.org/library/datetime.html#datetime.datetime