【发布时间】:2012-05-18 06:50:39
【问题描述】:
我想开发一个带有动态网址的网络服务。
具有命名组的常见 url 模式是这样的:
(r'^articles/(?P<year>\d{4})/(?P<month>\d{2})/$', 'news.views.month_archive')
出于我的目的,我需要更多动态,因为我从一开始就不知道组的数量。
例如可能的网址可能是:
- 文章/帖子/cmets
- 文章/帖子/作者
- 文章/帖子/ ...
我可以使用这样的东西:
(r'^(?P<cat1>)/(?P<cat2>)/$', 'module.views.function')
(r'^(?P<cat1>)/(?P<cat2>)/(?P<cat3>)/$', 'module.views.function')
(r'^(?P<cat1>)/(?P<cat2>)/(?P<cat3>)/(?P<cat4>)/$', 'module.views.function')
.. 等等。有没有更聪明的方法来做到这一点?提前致谢!
【问题讨论】:
-
这让我很感兴趣,我之前就想要它,但从来没有想出一个好的解决方案
标签: regex django url-pattern