Web2py:自定义Auth Forms

时间:2015-03-25 15:02:41

标签: web2py

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

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


更新 - 使用安东尼的建议解决了这个问题。登录实现如下所示:

<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}}

1 个答案:

答案 0 :(得分:4)

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

最简单的方法是编写formstyle函数(例如,将其称为auth_formstyle),然后执行:

auth.settings.formstyle = auth_formstyle

或者,您可以按照here所述的方法在视图中使用HTML自定义表单。您还可以编写完全自定义的HTML - 在这种情况下,只需确保表单输入名称与服务器上使用的字段名称匹配,并将结束</form>标记替换为{{=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}}
相关问题