【问题标题】:Web2py: Customizing Auth FormsWeb2py:自定义身份验证表单
【发布时间】:2015-03-25 15:02:41
【问题描述】:

我正在尝试自定义登录、注册、忘记用户名和重置密码表单的外观。我首选的解决方案是直接使用 HTML 表单,这可能需要创建新的函数和视图。此时,我不需要自定义字段。

不完全确定如何完成这项工作(尤其是如何处理从表单提交的数据)。有没有人有完成这项工作的经验,可以分享他们的方法?


更新 - 使用 Anthony 的建议解决了这个问题。登录实现如下图:

<form class="form-horizontal" method="post" style="margin-top:30px;">
  <div class="form-group">
    <label for="auth_user_username" class="col-sm-2 control-label">Username</label>
    <div class="col-sm-10">
      <input type="text" class="form-control" id="auth_user_username" name="username" placeholder="Enter username">
    </div>
  </div>
  <div class="form-group">
    <label for="auth_user_password" class="col-sm-2 control-label">Password</label>
    <div class="col-sm-10">
      <input type="password" class="form-control" id="auth_user_password" name="password" placeholder="Enter password">
    </div>
  </div>
  <div class="form-group">
    <div class="col-sm-offset-2 col-sm-10">
      <div class="checkbox">
        <label>
          <input id="auth_user_remember_me" name="remember_me" type="checkbox"> Remember me
        </label>
      </div>
    </div>
  </div>
  <div class="form-group">
    <div class="col-sm-offset-2 col-sm-10">
      <button type="submit" class="btn btn-default">Login</button>
    </div>
  </div>
{{=form.custom.end}}

【问题讨论】:

    标签: web2py


    【解决方案1】:

    您仍应在服务器端使用标准的Auth 表单——然后只需自定义字段在视图中的显示方式。有多种方法可以自定义表单(与任何其他 SQLFORM 相同的原则适用于 Auth 表单)。

    最简单的方法是写一个formstyle函数(例如,叫它auth_formstyle),然后:

    auth.settings.formstyle = auth_formstyle
    

    或者,您可以按照here 描述的方法在视图中使用 HTML 自定义表单。您还可以编写完全自定义的 HTML——在这种情况下,只需确保表单输入名称与服务器上使用的字段名称匹配,并将结束 &lt;/form&gt; 标记替换为 {{=form.custom.end}}(包括隐藏字段 web2py需要表单处理和 CSRF 保护)。

    如果您继续对所有表单使用单个 user 操作和视图,则可以使用视图中的逻辑根据特定的 Auth 操作显示不同的表单:

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

    在 /default/user.html 视图中:

    {{if request.args(0) == 'login':}}
    [custom login form markup]
    {{elif request.args(0) == 'register':}}
    [custom register form markup]
    ...
    {{pass}}
    

    【讨论】:

    • {{=form.custom.end}} 似乎非常适合我的需求。我用登录实现更新了问题,但其余的似乎也很好。
    猜你喜欢
    • 1970-01-01
    • 2015-07-02
    • 2018-08-28
    • 1970-01-01
    • 1970-01-01
    • 2020-04-01
    • 2022-12-10
    • 1970-01-01
    相关资源
    最近更新 更多