递归网址格式CMS风格 [英] Recursive URL Patterns CMS Style

查看:102
本文介绍了递归网址格式CMS风格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我学习一门新的语言/框架,我总是做一个内容管理系统...



我正在学习Python和Django,我被困住了一个URL模式,可以选择正确的页面。



例如,对于单级URL模式,我有:

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

哪些适用于像这样的网址:

  http:// localhost:8000 / page / 

现在,我不知道我是否可以让Django的URL系统收回s s a a::::>>>>>>>>>>>>>>>>>>>>>> c c c c > http:// localhost:8000 / parent / child / grandchild /

将返回父,孙子。



Django是否已经有这样的东西?还是修改我的原始网址格式以允许斜杠和提取URL数据?



感谢您提前的帮助。

解决方案

这是因为你的正则表达式不允许中间的'/'字符。 url段模式的递归定义可能是可能的,但无论如何,它将作为块传递给您的查看函数。



尝试这个

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

并分割参数传递给 page_by_slug() by'/',那么你将得到 ['parent','child','grandchild'] 。我不知道您是如何组织页面模型的,但如果没有太多的错误,请考虑使用或改进已经包含在Django中的平板包。



请注意,如果您有其他类型的网址不表示用户生成的页面,但系统自己的页面,您应该将它们放在您列出的模式之前,因为Django的URL匹配机制遵循给定的顺序。 p>

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 that will pick the right page.

For example, for a single-level URL pattern, I have:

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

Which works great for urls like:

http://localhost:8000/page/

Now, I'm not sure if I can get Django's URL system to bring back a list of slugs ala:

http://localhost:8000/parent/child/grandchild/

would return parent, child, grandchild.

So is this something that Django does already? Or do I modify my original URL pattern to allow slashes and extract the URL data there?

Thanks for the help in advance.

解决方案

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.

这篇关于递归网址格式CMS风格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆