【问题标题】:when using web2py ,why the url changed automatically when using auth使用 web2py 时,为什么使用 auth 时 url 会自动更改
【发布时间】:2015-12-24 09:55:35
【问题描述】:

我在控制器文件(test.py)中定义了一个函数 代码是:

def user():
    return dict(form=auth())

然后,当我访问http://localhost:8000/demo/test/user 为什么url会自动变成http://localhost:8000/demo/default/user/login

【问题讨论】:

    标签: python web2py


    【解决方案1】:

    当您通过 auth() 调用 Auth 对象时,它会检查 URL 参数(即,在控制器和函数之后的 URL 部分)以确定请求了哪个 Auth 方法(例如,登录、注册、个人资料等)。如果没有 URL arg(如您的情况),那么它将重定向到与当前 URL 相同的 URL,但附加 /login (否则,如果没有请求任何特定的 Auth 方法,它将没有任何返回)。

    如果您要使用上述构造(即,简单地调用 auth() 的通用 user 函数),那么您应该创建包含特定 Auth 方法作为第一个 URL 参数的链接。如果出于某种原因您希望登录链接为 /user(没有任何 URL arg),您可以执行以下操作:

    def user():
        if not request.args:
            form = auth.login()
        else:
            form = auth()
        return dict(form=form)
    

    当没有 URL 参数时,这将显式返回登录表单,否则将回退到标准行为。

    您当然也可以对每个 Auth 方法有完全独立的操作:

    def login():
        return dict(form=auth.login())
    
    def register():
        return dict(form=auth.register())
    
    etc.
    

    【讨论】:

      猜你喜欢
      • 2021-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多