开发者

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

开发者 https://www.devze.com 2023-01-29 21:15 出处:网络
In my URLconf I would like to be able to pass no time at all, a date or a date + time like so: /posts/

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

精彩评论

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