Monday 12 September 2011

Web2Py - How to create custom view for register form, login form, profile form and even reset password form?

How to create custom view for register form, login form, profile form and even reset password form in Web2Py?

If you want to manage and customize the authentication forms in web2py.

Let's say you wanna add more fields and make the profile more complete.
You need to overload the default user table and to add more fields to it. After that you have to create, controllers and views for login, register, profile , password reset.

1 - open your db file in models (by default db.py) and add the following code

db.define_table(

    auth.settings.table_user_name,

    Field('first_name', length=128, default=''),

    Field('last_name', length=128, default=''),

    Field('email', length=128, default='', unique=True),

    Field('address', length=256, default=''),

    Field('postcode', length=128, default=''),

    Field('city', length=128, default=''),

    Field('country', length=128, requires=IS_IN_SET(COUNTRIES)),


    Field('password', 'password', length=512, readable=False, label='Password'),

    Field('registration_key', length=512, writable=False, readable=False, default=''),

    Field('reset_password_key', length=512, writable=False, readable=False, default=''),

    Field('registration_id', length=512, writable=False, readable=False, default=''),

    format='%(first_name)s %(last_name)s')


member = db[auth.settings.table_user_name] # get the custom_auth_table

member.first_name.requires = \

IS_NOT_EMPTY(error_message=auth.messages.is_empty)

member.last_name.requires = \

IS_NOT_EMPTY(error_message=auth.messages.is_empty)

member.password.requires = [IS_STRONG(min=5, special=0, upper=0), CRYPT()]

member.email.requires = [

IS_EMAIL(error_message=auth.messages.invalid_email),

IS_NOT_IN_DB(db, 'auth_user.email')]



2 - How to make custom register form, profile form, login form and even reset password form

in your controller (dafault.py) add new function


def register():

     return dict(form=auth.register())


create new view (register.html) in your views/default folder:


{{extend 'layout.html'}}

<div id="reset_password">

Please fill the form<br/>

{{=form}}

</div>


2 - How to make custom profile form
CONTROLLER

def profile():

     return dict(form=auth.profile())




VIEW

{{extend 'layout.html'}}

<div id="profile_user">

Please fill the form<br/>

{{=form}}

</div>


3 - How to make custom login form
CONTROLLER

def login():

     return dict(form=auth.login())

VIEW

{{extend 'layout.html'}}

<div id="login_form">

Please fill the form<br/>

{{=form}}

</div>


4 -  How to make custom reset password form

CONTROLLER

     def resetpass():

return dict(form=auth.request_reset_password())


VIEW

{{extend 'layout.html'}}

<div id="reset_password">

Please fill the form<br/>

{{=form}}

</div>



This is simple form inside your default layout. You can style it with CSS. You can also add more divs and split the content/user fields in different vids.
This can be done with using  -  form.custom and form.custom.widgets. instead of default {{=form}}
Example:
VIEW

{{=form.custom.begin}}

<div id="user_main">

      First name:

      <div>{{=form.custom.widget.first_name}}</div>

      Last name:

      <div>{{=form.custom.widget.last_name}}</div>

      Gender:

      <div>

      {{=form.custom.widget.gender}}

      </div>

</div>

<div id="user_contact">

      Address:

      <div>{{=form.custom.widget.address}}</div>

      Postcode:

      <div>{{=form.custom.widget.postcode}}</div>

      Country:

      <div>{{=form.custom.widget.country}}</div>

      City:

      <div>{{=form.custom.widget.city}}</div>

</div>