如何使用尽可能短的URL路由单个web2py应用程序而不会丢失管理应用程序

时间:2016-02-19 05:43:25

标签: python web-applications routes web2py

我有一个web2py网站,它有两个应用程序:标准管理应用程序和自定义'myapp'应用程序。为了便于讨论,我们将说网站位于'sub.projdomain.com'。我已经尝试了几个小时来绕过路线和python正则表达式,但我似乎无法确定我想要的功能。我试图复制以下行为:

routes_in = (
    #Map the default index to '/'
    ('/', '/myapp/default/index'),
    #Allow the admin application to be accessed
    ('/a', '/admin/default/index'),
    ('/a/$anything', '/admin/$anything'),
    #Re-wire actions as controllers unless the controller is also specified
    ('/$controller', '/myapp/$controller/index'),
    ('/$controller/', '/myapp/$controller/index'),
    ('/$controller/$action', '/myapp/$controller/$action'),
    #Map static access to our app
    ('/static/$anything', '/myapp/static/$anything'),
)
routes_out = [(x, y) for (y, x) in routes_in]

我似乎总是遇到问题,我从“sub.projdomain.com/foo/bar/”页面调用URL('login')或URL('/ login')并获取表单的错误URL “sub.projdomain.com/foo/login/”或类似的东西。目前我正在设置'routes_in'和'routes_out'元组,因为我无法使用'router'。我觉得我忽略了一条非常简单的路线。我是否在思考这个问题?

----更新----

好吧,我想我设法找出了一套路线来获得我想要的行为,但我真的想知道这是否是实现它的正确方法。

1)
  CURSOR get_cost_ IS
  SELECT EQUIP, COST
  FROM   EXIPMNT
  WHERE  mch_code = mch_code_
  AND contract = contract_
  AND CRE_DATE BETWEEN TO_DATE('01/01/' || year_, 'DD/MM/YYYY')
  AND TO_DATE('01/01/' || year_, 'DD/MM/YYYY') + INTERVAL '1' YEAR;

BEGIN

FOR rec_ IN get_cost_ LOOP
  IF (rec_. EQUIP = 'M') THEN
     material_ := material_ + nvl(rec_.cost, 0);
  ELSIF (rec_. EQUIP = 'P') THEN
     personal_ := personal_ + nvl(rec_.cost, 0);
  ELSIF (rec_. EQUIP = 'T') THEN
     tool_facility_ := tool_facility_ + nvl(rec_.cost, 0);
  ELSIF (rec_. EQUIP = 'X') THEN
     expense_ := expense_ + nvl(rec_.cost, 0);
  ELSIF (rec_. EQUIP = 'E') THEN
     external_ := external_ + nvl(rec_.cost, 0);
  ELSIF (rec_. EQUIP = 'D') THEN
     direct_sales_ := direct_sales_ + nvl(rec_.cost, 0);
  END IF; 

 NULL;
 END LOOP;

此设置要求我仔细构建传递给URL(..)的参数。我希望更容易预测哪些URL(..)产生,但我怀疑它比这更简单......任何帮助都表示赞赏!

2 个答案:

答案 0 :(得分:0)

您应该能够使用parameter-based rewrite system获得大致相同的结果:

routers = dict(
    BASE=dict(
        default_application='myapp',
        default_controller='default',
        default_function='index',
        functions=dict(
            default=['list', 'of', 'functions', 'in', 'default.py'],
            foo=['list', 'of', 'functions', 'in', 'foo.py']
        )
    ),
    admin=dict(
        default_function='site',
        functions=['list', 'of', 'functions', 'in', 'default.py']
    )
)

唯一的区别是" admin"应用程序仍然需要" admin"在URL中(虽然我想你可以通过将其文件夹重命名为" a")来尝试更改应用程序本身的名称。

注意,如果要删除" index"您只需要在控制器中列出functions。 (或者#34;站点"在管理员的情况下)当URL附加了额外的args时(路由器需要能够区分URL args和控制器功能)。

答案 1 :(得分:0)

好吧,我想我想出来了。我还没有遇到过URL()无法生成正确字符串的情况。我相信这可以进一步简化,但在这里:

routes_in = (
    #Map the default index to '/'
    ('/', '/myapp/default/index'),
    #Allow the admin application to be accessed
    ('/a', '/a/default/index'),
    ('/a/$anything', '/a/$anything'),
    #Re-wire actions as controllers unless the controller is also specified
    ('/$controller', '/myapp/$controller/index'),
    ('/$controller/', '/myapp/$controller/index'),
    ('/$controller/$action', '/myapp/$controller/$action'),
    #Map static access to our application
    ('/static/$anything', '/myapp/static/$anything'),
)
#Reverse only some of the mappings so that URL() works correctly
routes_out = (
    ('/myapp/default/index', '/'),
    ('/a/$anything', '/a/$anything'),
    ('/myapp/$controller/index', '/$controller'),
    ('/myapp/$controller/$action', '/$controller/$action'),
    ('/myapp/static/$anything', '/static/$anything'),
)

我重新命名了' admin'申请到' a。无论何时我调用URL(),我必须明确传递2个参数。第一个参数始终是控制器的名称,第二个参数始终是任何GET请求变量后面的操作的名称。例如。主要索引页面为URL('default', 'index')URL('foo', 'bar?param=3')bar foo指定了param。但是,不允许URL('foo', '?indexParam=test'),因为在第二个参数中的变量之前未指定index。只要我遵循这些规则,一切似乎都能保持一致。

我对任何其他建议持开放态度,但这是我接受的答案。我希望这有助于其他人经历同样的困境。