开发者

What is the best way to get a datetime or date in a urlconf in Django

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-开发者_JAVA技巧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?


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:

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


See http://docs.djangoproject.com/en/dev/topics/http/urls/#named-groups . Likely you'll need something like :

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

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜