Web2py:自定义身份验证表单 [英] Web2py: Customizing Auth Forms

查看:24
本文介绍了Web2py:自定义身份验证表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

<小时>

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

<div class="form-group"><label for="auth_user_username" class="col-sm-2 control-label">用户名</label><div class="col-sm-10"><input type="text" class="form-control" id="auth_user_username" name="username" placeholder="输入用户名">

<div class="form-group"><label for="auth_user_password" class="col-sm-2 control-label">密码</label><div class="col-sm-10"><input type="password" class="form-control" id="auth_user_password" name="password" placeholder="输入密码">

<div class="form-group"><div class="col-sm-offset-2 col-sm-10"><div class="checkbox"><标签><input id="auth_user_remember_me" name="remember_me" type="checkbox">记住账号

<div class="form-group"><div class="col-sm-offset-2 col-sm-10"><button type="submit" class="btn btn-default">登录</button>

{{=form.custom.end}}

解决方案

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

最简单的方法是编写一个formstyle函数(例如,调用它auth_formstyle),然后执行:

auth.settings.formstyle = auth_formstyle

或者,您可以遵循 在这里 在视图中使用 HTML 自定义表单.您还可以编写完全自定义的 HTML —— 在这种情况下,只需确保表单输入名称与服务器上使用的字段名称匹配,并将 </form> 结束标记替换为 {{=form.custom.end}}(包括 web2py 用于表单处理和 CSRF 保护所需的隐藏字段).

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

def user():返回 dict(form=auth())

在/default/user.html 视图中:

{{if request.args(0) == 'login':}}[自定义登录表单标记]{{elif request.args(0) == '注册':}}[自定义注册表单标记]...{{经过}}

I'm trying to customize the look-and-feel of our login, register, forgot username, and reset password forms. My preferred solution would be to use HTML forms directly, which may require creating new functions and views. At this point, I don't need custom fields.

Not exactly sure how to get this done (particularly what to do with the data submitted from the forms). Does anyone have experience getting this done, that can share their approach?


UPDATE - Solved this using Anthony's Suggestion. Login implementation shown below:

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

解决方案

You should still use the standard Auth forms on the server side -- then just customize how the fields are displayed in the view. There are various ways to customize forms (same principles apply to Auth forms as any other SQLFORM).

The simplest approach is to write a formstyle function (e.g., call it auth_formstyle), and then do:

auth.settings.formstyle = auth_formstyle

Alternatively, you can follow the approach described here to customize the forms using HTML in the views. You can also write completely custom HTML -- in that case, just be sure that the form input names match the field names used on the server, and also replace the closing </form> tag with {{=form.custom.end}} (that includes the hidden fields web2py needs for form processing and CSRF protection).

If you continue using a single user action and view for all forms, you can use logic in the view to display different forms depending on the specific Auth action:

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

In the /default/user.html view:

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

这篇关于Web2py:自定义身份验证表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆