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

发布于 2024-10-06 20:41:08 字数 225 浏览 7 评论 0原文

在我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蹲墙角沉默 2024-10-13 20:41:08

我建议还用斜杠分隔年/月/日,这样可以更轻松地集成(也许稍后也可以)视图,例如。显示年度/每月概览:

r'^(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d{2})/(<?P<hour>\d{2})-(<?P<minute>\d{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:

r'^(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d{2})/(<?P<hour>\d{2})-(<?P<minute>\d{2})/$
彼岸花似海 2024-10-13 20:41:08

请参阅 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})

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

, 'posts'),

注意这是未经测试的,您仍然需要在视图中将其设为日期时间对象,要转换为日期时间,请参阅 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 :

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

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

, '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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文