在Django中获取urlconf中的日期时间或日期的最佳方法是什么

时间:2010-12-11 17:25:29

标签: django django-views django-urls

在我的URLconf中,我希望能够完全没有时间,日期或日期+时间如下:

/帖/

/帖/ 2010-01-01

/ posts / 2010-01-01 20:30

在我看来,我想创建一个datetime.datetime对象或一个datetime.date对象

在urlconf中写这个的最好方法是什么?

2 个答案:

答案 0 :(得分:1)

我建议使用斜杠分隔年/月/日,这样可以更容易地集成(也可能是稍后)视图,例如。显示年度/月度概述:

r'^(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d{2})/(<?P<hour>\d{2})-(<?P<minute>\d{2})/$

答案 1 :(得分:0)

http://docs.djangoproject.com/en/dev/topics/http/urls/#named-groups。可能你需要像:

(r'^posts/(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d+)/(<?P<hour>\d{2})-(<?P<minute>\d{2})$', 'posts'),

请注意,这是未经测试的,您仍然需要在视图中将其设为日期时间对象,以便转换为日期时间,请参阅http://docs.python.org/library/datetime.html#datetime.datetime