Table of contents
1.
Introduction
2.
Adding extra form elements to SQLFORM
3.
SQLFORM without database IO
4.
SQLFORM without database IO Insertion
5.
SQLFORM without database IO Updating and Deletion
6.
Frequently Asked Questions
6.1.
Describe a case when we may need SQLFORM without database IO.
6.2.
Is web2py an MVC framework?
6.3.
Which is better, web2py or Django?
6.4.
What is Database Abstraction Layer?
6.5.
Why web2py?
7.
Conclusion
Last Updated: Mar 27, 2024
Easy

Extra form elements and SQLFORM without database IO in web2py

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

A web application is a software or a collection of instructions run when a client accesses a specified URL. Web applications are created using a variety of programming languages, but beginning from scratch may be time-consuming. We make use of the Web Frameworks for this purpose.

web2py

One of the most well-known web frameworks is the Web2Py framework. While developing web content, we may want to add an extra element to your form after it has been produced on occasion, or there are occasions when we want to use SQLFORM to create a form from a database table and validate a submitted form. Still, we don't want any automatic INSERT/UPDATE/DELETE in the database.

In this article, we will discuss the methods Web2py provides for adding extra form elements to SQLFORM, generating a form from a database table using SQLFORM and validating the submitted form accordingly, without any automatic INSERT/UPDATE/DELETE in the database.

Adding extra form elements to SQLFORM

Image of SQL form

We may want to add an extra feature to our form after it has been developed. For example, we may want to include a checkbox that certifies the user agrees to your website's terms and conditions:

form = SQLFORM(db.yourtable)
my_extra_element = TR(LABEL('I accept the terms and conditions),
                      INPUT(_name='agree', value=True, _type='checkbox'))
form[0].insert(-1, my_extra_element)

 

In the above example, my_extra_element should be modified to match the form style, and the default formstyle='table3cols' was assumed. After submission, form.vars.agree will have the checkbox state, which may subsequently be used in an “onvalidation” function.

SQLFORM without database IO

SQLFORM Coding Ninjas

Sometimes we want to utilise SQLFORM to generate a form from a database table and validate a submitted form. Still, we don't want any automatic INSERT/UPDATE/DELETE in the database.

The above is true, for example, when one of the fields must be computed based on the values of the other input fields. This is also true when you perform additional validation on the inserted data that standard validators cannot provide.

SQLFORM without database IO Insertion

The SQLFORM without database IO Insertion can be simply accomplished by breaking:

form = SQLFORM(db.person)
if form.process().accepted:
    response.flash = 'record inserted'

 

into

form = SQLFORM(db.person)
if form.validate():
    ### deal with uploads explicitly


    form.vars.id = db.person.insert(**dict(form.vars))


    response.flash = 'record inserted'

SQLFORM without database IO Updating and Deletion

The updating or deletion of SQLFORM without database IO can be simply accomplished by breaking:

form = SQLFORM(db.person, record)
if form.process().accepted:
    response.flash = 'record updated'

 

into

form = SQLFORM(db.person, record)
if form.validate():
    if form.deleted:
        db(db.person.id==record.id).delete()
    else:
        form.record.update_record(**dict(form.vars))
    response.flash = 'record updated'

 

In the case of a table with an "upload"-type field ("fieldname), both process(dbio=False) and validate() handle the upload file storage as if process(dbio=True), which is the default behaviour.

The name given to the uploaded file via web2py can be found in:

form.vars.fieldname

Frequently Asked Questions

Describe a case when we may need SQLFORM without database IO.

When one of the fields needs to be computed from the values of other input fields, this is one of the cases where SQLFORM without database IO is required.

Is web2py an MVC framework?

The Ruby on Rails and Django frameworks informed the creation of web2py. Web2py, like similar frameworks, emphasises rapid development, prefers convention over configuration, and adheres to the model-view-controller (MVC) architectural paradigm.

Which is better, web2py or Django?

Due to its smaller size, short learning curve, and lack of project-level configuration files, web2py differs from Django. Compared to PHP-based frameworks and Java-based frameworks, web2py has a significantly more explicit syntax.

What is Database Abstraction Layer?

A database abstraction layer is an API (Application Programming Interface) that allows a computer application to communicate with databases such as Oracle, SQL Server, MySQL, IBM Db2, and PostgreSQL.

Why web2py?

Server-side web development is easy to learn and lightweight, and fast.

Conclusion

In this article, we have extensively discussed how to add extra form elements and SQLFORM without database IO in web2py. 

Do you not feel eager to read/explore additional information on the subject of Web2py after reading about how to add extra form elements and SQLFORM without database IO in web2py? See the Web2py, Web2Py init Application creationWeb2Py Installation, and Troubleshooting to learn more.

Nevertheless, you may consider our paid courses to give your career an edge over others!

Do upvote our blogs if you find them helpful and engaging!

Happy Learning!

Thank you image coding ninja
Live masterclass