How to create, insert and update a database table using web2py?

I am new to python and web2py development. My requirement:

I need to create one html page that consists of Employee information eg. employee name, age, address, etc. and the buttons will be there Save, Refresh .. After entering the employee information into the html page, if the user clicks the Save button, he must save the record in the Employee table (if it already exists) OR he will first create the table and save the data in it. and the refresh button will fetch data from Employee table and database in html page. web2py supports MVC architecture, so please provide me with some sample code on how to achieve it using MVC in web2py.

+3


source to share


1 answer


In web2py, you define your table structure in your model, something like this:

# Sample Projects Container
db.define_table('it_projects',
                db.Field('project_name', 'string', length=255, required=True),
                db.Field('description', 'text', required=False, default=''),
                db.Field('is_active', 'boolean', required=False, default=True),
                db.Field('created_on', 'datetime', required=True),
                db.Field('created_by', db.auth_users),
                db.Field('anonymous_read', 'boolean', required=True),
                migrate='it_projects.table')

      



Then you code your view containing your markup and in your controller you just insert the data into the table, I would suggest that you read the online version of web2py how it provides CRUD transaction information. Greetings.

+1


source







All Articles