Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
A portion of a page known as a form includes controls including text fields, password fields, checkboxes, radio buttons, a submit button, menus, etc. A form makes it easier for the user to input data like name, email address, password, phone number, etc., and that data will transfer to the server for processing. web2py handles forms uniquely, which involves powerful functions responsible for form generation.
There are four ways we can create forms in web2py- FORM, SQLFORM, SQLFORM.factory, and CRUD Methods. In the following article, we will dive deeper into other types of forms and look at their usage along with examples.
Form Generation
A simple form can be generated with the helpers like the FORM object.
Controller
The below function in “default.py” controller includes FORM object (HTML helper) which helps in creation of form.
def display_form():
#function to display form
form = FORM('Value:', INPUT(_value = 'name'), INPUT(_type = 'submit'))
return dict(form = form)
You can also try this code with Online Python Compiler
The form which is generated by the statement {{= form}} serializes the FORM object.
{{extend 'layout.html'}}
<h2>Basic form</h2>
{{= form}}
<h2>Submitted variables</h2>
{{= BEAUTIFY(request.vars)}}
You can also try this code with Online Python Compiler
The form self-submits when a user fills it out and hits the submit button, and the variable request.vars.value and its input value are displayed at the bottom.
SQLFORM.factory
Without a database, the user may occasionally need to create a form using an existing database table. The user merely wishes to benefit from the SQLFORM functionality.
This is accomplished via form.factory, and a session is kept.
There are the following three ways we can upload files with SQLFORM.factory namely-
One form for multiple tables
Confirmation forms
Form to edit A dictionary
One form for multiple tables
It frequently happens that you wish to develop a single form that allows users to enter information about one customer and their default address but you already have two tables, such as "client" and "address," connected together by a reference.
Take note of SQLFORM.factory (it makes ONE form using public fields from both tables and inherits their validators too). When a form is submitted, this performs two inserts of data—some into one table and some into the other.
Confirmation Forms
You frequently require a form with a confirmation option. If the option is selected, the form should be accepted—but only after submission. Additional options on the form may link to other websites. There is an easy way to achieve this with web2py-
form = FORM.confirm('Are you sure?')
if form.accepted: do_what_needs_to_be_done()
You can also try this code with Online Python Compiler
Keep in mind that since this is handled internally, the confirm form does not require and cannot call.accepts or.process. The confirmation form allows you to add buttons with links in the form of a dictionary of { "value": "link"}-
form = FORM.confirm('Are you sure?', {'Back':URL('other_page')})
if form.accepted: do_what_needs_to_be_done()
You can also try this code with Online Python Compiler
Each entry from the dictionary will have its own INPUT field on the form. Dictionary keys will be used as INPUT names, labels, and current values to deduce kinds (string, int, double, date, datetime, boolean).
This works excellent, but I'll leave the reasoning behind making the configuration dictionary persistent to you. For instance, you could want to keep the configuration in a session.
if not session.config:
session.config = dict(color='black', language='English')
form = SQLFORM.dictform(session.config)
if form.process().accepted:
session.config.update(form.vars)
You can also try this code with Online Python Compiler
Web2py is a top-notch framework, yes. The simplicity of usage, from installation to learning to code to distribution to deployment, is a key objective of web2py.
What is the use of web2py?
Python dynamic web content programming is made possible via Web2py. Web2py is made to make laborious web development jobs more manageable, such as creating web forms from scratch, while a web developer can still do it if necessary.
Is web2py an MVC model?
The Ruby on Rails and Django frameworks inspired the creation of web2py. Web2py is similar to these frameworks in that it emphasizes rapid development, prefers convention over configuration, and adheres to the model-view-controller (MVC) architectural pattern.
Does web2py support Python 3?
Web2py functions on Python 2.7 and Python 3 along with CPython (the C implementation) and PyPy (Python written in Python).
Which is better, web2py or Flask?
The majority of the Slant community suggests Flask when comparing web2py to that framework. What are the best backend web frameworks, as in the question? Web2py is ranked 19th, while Flask is placed fourth.
Conclusion
In this article, we have extensively discussed types of Forms in web2py. We began with a brief introduction to forms followed by their types and examples.
After reading about Other types of Forms in web2py, refer to the web2py web framework,