Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

web2py
web development
python

How to do Postbacks in Web2Py

Educative Answers Team

Postbacks

A Postback is the sending of an HTTP POST Request to the page where the form was in the first place.

Postbacks have many use-cases​, but they are most commonly used for form input validation.

Usage

To set Web2Py code to run on a certain IP address and port, the Web2Py file needs to be downloaded and run. The administrative interface then needs to be logged into and the application set up on it. Most Web2Py functions, including those used in this tutorial, do not need to be imported.

The default controller function needs to be set up to implement self-submission:

def first():
    if request.vars.email_address:
        session.email_address = request.vars.email_address
        redirect(URL('second'))
    return dict()

The HTML file also needs to be set up:

{{extend 'layout.html'}}
Email Address: 
<form>
  <input name="email_address" />
  <input type="submit" />
</form>

In this piece of code, the request.vars.email_address is the variable in the POST request. This variable is then stored in the session information as session.email_address.

Since forms need to be able to send data to other webpages as well, the HTML file of the next page also needs to be edited:

{{extend 'layout.html'}}
<h1> {{=session.email_address}} has logged in!</h1>

Since the Python function on the first page has been setup to send POST requests to itself, the session information is accessed on the second page so that information can be transferred between pages.

RELATED TAGS

web2py
web development
python
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring