Wednesday, 6 November 2013

WEB2PY Using Python



Steps to create a Simple Form in Web2Py


++Start the Web2PyServer

++
Enter in the Administration Interface.On the left hand side give a name of a new application say RegForm

++
The application we just created in Web2Py has one major file for now  that we need to be concerned about  in order to experiment with the web form and that is default.py in the Controller section.
This default.py is a Python controller that constructs the back-end operations for our form application and manages the data provided by it.

++
Next comes formation of a database in Web2Py

db = DAL('sqlite://storage.sqlite')
db.define_table('registrationform',
    Field('firstname', requires=IS_NOT_EMPTY()),
    Field('lastname', requires=IS_NOT_EMPTY()),
    Field('gender', requires=IS_IN_SET(['Male', 'Female'])),
    Field('username', requires=IS_NOT_EMPTY()),
    Field('password', 'password')
   
Once this default.py is saved, Web2Py controller reads this database schema, and constructs a table named ‘registrationform’ having the following fields:
firstname
lastname
gender
username
password

The code  (db = DAL(‘sqlite://storage.sqlite’) defines the database abstraction layer (DAL()) for the Web2Py controller, and based on the input parameters provided, it establishes the connection with the defined database.
Because, SQLite comes by default with Web2Py, so we can utilize it (although this can be replaced by any type of DBMS like MySQL, etc.).
On the other hand, the function right below it (db.define_table()) defines the structure of the newly created database table.


++
Now we can create our FORM fields.
In the same default.py, enter this new function at the end of the file  right below the above code:
def reg_form():
    form = SQLFORM(db.registration)
    return dict(form=form).
SQLFORM() function in Web2Py ,simply extracts all the input fields defined in the ‘registration’ table .


++
 All we need to do now is to call it  in an html template/page.
In the Views section of the forms application page, create a new html file named default/reg_form.html (it is recommended that we keep the html file name as default/[name of the function] that we specified in Controller default.py).
Open this newly created html file and enter the following HTML code :

<h1>Registration Form </h1>
<br  />
{{=form}}
<br  />

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


A good tutorial to create a Simple app :http://blog.fruiapps.com/2012/05/Web2py-tutorial-to-create-a-Notes-Application

Another  good tutorial video is : http://vimeo.com/3703345

No comments:

Post a Comment