Python and Web: Building Dynamic Web Forms in Web2Py from Ground – Part I

Under the particular domain of web development, Python has offered much more than we expected it to in the early 2000’s. It now offers multiple pre-built web frameworks that even a novice coder can utilize for the development of complex applications. Today, we will witness it. Today, we will go through the process of building a dynamic web form using one of its frameworks called Web2Py on a local machine/server.

In the progression of this tutorial, you will be able to learn how dynamic web forms are created, how their field validation schemes work, and how easily they are linked with the database under the hood of Web2Py. This tutorial (however) assumes that:

Starting the Server:

In order to start your Web2Py server, cd to the unzipped web2py folder using command line and type:

> web2py.exe localhost:8000 --password [your new password]

You will see a notification message once you hit enter after typing this command. At this point, verify the server initiation by typing http://localhost:8000 in your browser and if you see a welcome page, you are good to go forward.

Creating Form Application:

Browse to the administrative interface on the Web2Py welcome page and create a new application from the right hand section and name it as forms. We will use this application to experiment with our dynamic web form. Now, at this point, you need to understand some basics. The application you just created in Web2Py has only major file (for now) that you need to be concerned of in order to experiment with the web form, which is default.py in the Controller section. This default.py is a Python controller that constructs the back-end operations for your form application and manages the data provided by it.

Building Form:

Begin with the construction of your web form by opening up the default.py file (in the edit mode) and define a new database scheme as follows (at the end of the file):

Once this default.py is saved, Web2Py controller reads this database schema, and based on that, constructs a table named ‘registration’ having the following fields:

firstname

lastname

gender

username

password

about_you

The first line in the above 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. Since SQLite comes by default with Web2Py, so we will 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.

Once you are there, now its time to create the form fields. In the same default.py, enter this new function at the end of the file (right below the database code):

def form_a():
form = SQLFORM(db.registration)
return dict(form=form)

You have just created a nice looking registration form using the SQLFORM() function in Web2Py, which simply extracts all the input fields defined in the ‘registration’ table (isn’t it cool?). All you need to do now is to call it all in an html template/page.

Calling the Form:

In the Views section of the forms application page, create a new html file named default/form_a.html (it is recommended that you keep the html file name as default/[name of the function] that you specified in default.py). Open this newly created html file and make it read like this:

So-called ‘Dynamism’:

Alright, lets add some ‘dynamism’ to our web form so that it notifies the user about what fields are mandatory and additionally, shows the submitted data to the user. To do so, make your form_a() function look something like this:

Conclusion:

The process is very simple: We define the database table with all its fields (using db.define_table()) and call these fields into the Web2Py’s form through a separate Python function, which constructs them as form input fields. Once we have the form fields ready to be published, we import them on a new/existing html ‘views’ template by simply calling the form function under the html tags.

In the next article of this series, we will talk about some more advanced features for these web forms (just to give you a hint) like database validation, custom field creation, database update/delete/insert functions and few other cool functions – all using Web2Py’s Pythonic powers!

Saifuddin Abdullah is a tech writer and a Python based desktop/web applications developer working from New York City. His key areas of interest include mining, illustration and statistical evaluation of raw data using multiple programming applications.
More articles by Saifuddin Abdullah

Robert, F.

Excellent approach..actually I am surprised since I have always used Django but now I see that web2py has a lot more to offer as well. waiting for the next piece.

Hi I’m new to web2py, python and mvc, I like your tutorial but I’m a little confused, you say to construct the table in the controller default.py, but from what I’ve read so far db connections and defining tables should be in the Model db.py. Which is the correct approach?
Thanks for your help
David

Hey WordPress lovers. Every month dozens of incredible free WordPress plugins are released by the community, and we’re here to bring them to you. This seems to be a month of small but useful utilities that only do one or two things, with a few exceptions. So go on. March on down to the list, and get to it! (I'm only a little bit ashamed of that pun.) WP NoteUp ... Notes! Alongside your posts and pages! Use them to remind yourself, or other authors, about things you'd put in a note. That's really all there is to...

WordPress plugins are coming out all of the time, right and left. Every day, they’ve got new ones in the WordPress plugin repository for the curious developer to check out. Over time, your average designer or developer will end up with a list of their favorites. Usually, they’re plugins that solve common problems, are up-to-date and well-maintained, with good support. With that in mind, we’re sharing a list of some of the best plugins out there. Disable Comments ... Disable Comments does......

When I first started out creating websites with WordPress, I was in awe of the depth and variety of plugins available. During those golden days I would willingly install just about anything that looked cool. Shortly thereafter, I was smacked in the head by reality. Stuff started breaking and seriously hurt my good time. It turns out that not every plugin is a great one. And there are times when it seems like the work we do is held together with bubble gum and duct tape. Everything works splendidly until it...

WordPress is undoubtedly one of the most popular tools on the Internet because it makes it so easy to get a functional web site up and running in such a short time. And then there are the plugins. If you want your WordPress site to do something then odds are there is a plugin that you can install to make your site do what you want it to do. But plugins pose a bit of a problem at times. Since they are created by third-party developers you never know what quality control practices are used and what security testing...

WordPress beginners have difficulty customizing their websites past simple text changes, especially if they have no background knowledge of HTML or CSS. WordPress page builder plugins allow just about anyone to build a stylish or professional looking web page, through a user-friendly interface - quickly, and without knowing HTML or CSS. Though it may seem like WordPress page builder plugins have usefulness mainly for the technically unsavvy, they can also make a WordPress developer’s job...

Oftentimes, setting up a WordPress website results in a haphazard scramble to configure enough of the right plugins to make the client happy, and address any specific needs they might have mentioned. But a failure to install certain plugins means the client will come back at you with questions as to why it wasn’t done in the first place. Though you’re probably happy to rectify the problem (and collect a little extra money on the project), going back in to install plugins can distract from more profitable...

In Part 1 of this series we took a look at how you can better secure your WordPress files during, and after, the installation of the software. But once you have the files hardened against different threats, it is time to start looking at some of the different plug-ins available that can help you further protect and secure your WordPress site. WordPress Firewall 2 ... Having worked with a company that deals with web application firewalls, I can tell you from firsthand experience that these are by far one...

There’s a goal behind every website, and a specific chain of events that must occur before each goal is accomplished. Whether you want someone to sign up for your email newsletter, or fill out a contact form, these things don’t happen without the proper triggers. Used strategically, WordPress plugins can assist with the realization of important goals. Here’s how to turn your website into a conversion machine with WordPress Plugins. Essential Grid ... Essential Grid is an all-around grid solution...

About

DeveloperDrive.com is a blog about web development from the makers of the popular web design blog WebdesignerDepot.com
Founded in 2011, we focus on the latest trends, tutorials, opinion articles as well as tips and tricks to empower our readers to become better web developers.