开发者

Change the view prefix in pyramid traversal from "@@" to "+"

I am looking at moving a web app from pylons to pyramid (formally repoze.bfg) because traversal will fit my app much better than url dispatch.

Currently, when I have a obj with a number of views, I have the view names prefixed with a '+' in the url. e.g.:

/path/to/obj/   (default view)
/path/to/obj/+custom_view1
/path/to/obj/+custom_view2
/path/to/obj/+edit
/path/to/obj/+delete
/path/to/obj/sub_obj/

Pyramid has a feature to handle this is a nice way, but it uses the prefix "@@". Is there a way to change this to "开发者_如何学运维+" so that I can keep my urls the same (you know what they say about cool urls,) and use this feature.


Yes and no, you can change view prefix from "@@" but the new prefix should also be 2-symbold length, see sources for traverser.

Subclassing ResourceTreeTraverser and then registering it instead of the default one should do the trick:

from pyramid.traversal import ResourceTreeTraverser as BaseResourceTreeTraverser

class ResourceTreeTraverser(BaseResourceTreeTraverser):
    VIEW_SELECTOR = "++"

from pyramid.config import Configurator
from pyramid.interfaces import ITraverser
from zope.interface import Interface

config = Configurator()
config.registry.registerAdapter(ResourceTreeTraverser, (Interface,), ITraverser)

Personally, I think VIEW_SELECTOR should be refactored to allow any length view prefixes.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜