本文介绍了金字塔:如何创建使用自定义调度程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

除了将发送到视图的根 url 请求之外,我还希望有一个调度程序,我会将所有请求路由到不同的视图,我将根据自定义调度程序的规则选择这些视图.

Besides the root url request, which will be sent to a view, I want to have a dispatcher that I will route all the requests to different views which I will chose depending on my custom dispatcher's rules.

如何创建调度员?我一遍又一遍地阅读文档,但我无法弄清楚.

How can I create a dispatcher? I have read the docs over and over again yet I can not figure it out.

我想解析 url 的 *remainder,然后相应地分派请求.

I want to parse the *remainder of url and then dispatch the request accordingly.

推荐答案

这实际上取决于结构或您的 URLS 和您的自定义调度程序规则",但在许多情况下,您可以使用 URL 遍历而不是 URL 调度来实现什么你要.由于 URL 遍历使用了父资源的 __getitem__ 方法,可以编写普通的 Python 代码,这样可以给你更大的灵活性.

It really depends on the structure or you URLS and your "custom dispatcher rules", but for many cases you can use URL traversal instead of URL dispatch to achieve what you want. Due to the fact that URL traversal uses __getitem__ method of the parent resource, where you can write normal Python code, it may allow you more flexibility.

示例:假设您有以下网址:

Example: imagine you have the following URLS:

/food/banana
/food/potato
/food/tomato
/food/apple

并且您想对水果和蔬菜有不同的看法.你可以这样做:

and you want to have different views for fruit and vegetables. You can do something like this:

class FoodResource(object):
    def __getitem__(self, name):
        if name in ["banana", "apple"]:
            return FruitResource(self, name)
        if name in ["potato", "tomato"]:
            return VegetableResource(self, name)

然后你可以注册FruitResourceVegetableResource的视图:

then you can register views for FruitResource and VegetableResource:

@view_config(context=FruitResource):
def view_fruit(context, request):
    ...

@view_config(context=VegetableResource):
def view_vegetable(context, request):
    ...

您可以为 Fruits 和蔬菜注册不同的视图集,因此 /foor/banana/make_jam/food/apple/make_jam 将是水果的有效 URL仅,对于蔬菜,您将拥有,例如,/food/potato/make_soup:

You can have different sets of views registered for Fruits and Vegetables, so /foor/banana/make_jam and /food/apple/make_jam will be vaild URLs for fruits only, and for vegetables you'll have, say, /food/potato/make_soup:

@view_config(context=FruitResource, name="make_jam"):
def view_fruit_make_jam(context, request):
    ...

@view_config(context=VegetableResource, name="make_soup"):
def view_vegetable_make_soup(context, request):
    ...

然后你的 FruitResourceVegetableResource 可以有自己的 __getitem__ 方法,所以你可以有潜在不同的子项目"集 - /food/banana/skin/food/banana/flesh 等,并为其分配了自己的视图 - /food/banana/skin/peel, /food/banana/flesh/eat,其中 peeleat 是为虚构的 FruitSkinResource 和 FruitFleshResource.

Then your FruitResource and VegetableResource can have their own __getitem__ methods, so you can have potentially different sets of "subitems" - /food/banana/skin, /food/banana/flesh etc, with their own views assigned to them - /food/banana/skin/peel, /food/banana/flesh/eat, where peel and eat are views registered for imaginary FruitSkinResource and FruitFleshResource.

而且您可以拥有水果和蔬菜的自定义权限,因此访问 /food/apple/make_jam 可能需要一个权限,而 /food/potato/make_soup 需要另一个权限.

And you can have custom permissions for fruits and vegetables, so accessing /food/apple/make_jam may require one permission and /food/potato/make_soup another.

这篇关于金字塔:如何创建使用自定义调度程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 21:13