Recursive URL Patterns CMS Style

前端 未结 1 1702
长发绾君心
长发绾君心 2020-12-16 03:06

Whenever I learn a new language/framework, I always make a content management system...

I\'m learning Python & Django and I\'m stuck with making a URL pattern th

相关标签:
1条回答
  • 2020-12-16 03:29

    That's because your regular expression does not allow middle '/' characters. Recursive definition of url segments pattern may be possible, but anyway it would be passed as a chunk to your view function.

    Try this

    url(r'^(?P<segments>[-/\w]+)/$', views.page_by_slug, name='pg_slug'),
    

    and split segments argument passed to page_by_slug() by '/', then you will get ['parent', 'child', 'grandchild']. I'm not sure how you've organized the page model, but if it is not much sophiscated, consider using or improving flatpages package that is already included in Django.

    Note that if you have other kind of urls that does not indicate user-generated pages but system's own pages, you should put them before the pattern you listed because Django's url matching mechanism follows the given order.

    0 讨论(0)
提交回复
热议问题