English 中文(简体)
Web2py - Framework Overview
  • 时间:2024-09-17

Web2py - Framework Overview


Previous Page Next Page  

web2py is a full-stack web framework that can be used by a developer to completely develop a web apppcation. It includes SQL database integration and multi-threaded web server for designing a program.

Web Interface for Designing a User’s Program

Once the command is executed as per the operating system, web2py displays a startup window and then displays a GUI widget that asks the user to choose −

    a one-time administrator password,

    the IP address of the network interface to be used for the web server,

    and a port number from which to serve requests.

The administrator includes all the authority for addition and editing any new web apppcation.

By default, web2py runs its web server on 127.0.0.1:8000 (port 8000 on localhost) but a user can run it on any available IP address and port as per the requirement.

The web2py GUI widget will be displayed as shown below.

GUI Widget

The password is used in the administrative interface for any changes in the new module.

After the user has set the administration password, web2py starts up the web browser at the page with the following URL − http://127.0.0.1:8000/

The welcome page of the framework will be displayed as shown below.

Framework

Designing a Basic Program in web2py

After starting the web2py apppcation, with the above-mentioned URL, we can use the administrative interface for creating a new module, for example, “helloWorld”.

The administrative interface will ask for the password for authentication purpose as the administrator holds all the authority for addition and editing any new web apppcation.

Web2py Apppcations

The snapshot given above includes the page details, which psts all the installed web2py apppcations and allows the administrator to manage them. By default, the web2py framework comes with three apppcations. They are −

    An admin apppcation, which the user is implementing currently.

    An examples apppcation, with the onpne interactive documentation and an instance of the web2py official website.

    A welcome apppcation. It includes the basic template for any other web2py apppcation. It is also known as the scaffolding apppcation. The apppcation also welcomes a user at the startup.

Let the name of the new apppcation be “helloWorld”.

Once, a new apppcation is created, the user is redirected to a page comprising of view, model and controllers of the respective apppcation.

Edit Apppcation

The user can look at the newly created apppcation by mentioning the following URL − http://127.0.0.1:8000/helloWorld

By default, a user can view the following screen on hitting the above-mentioned URL.

For printing the message of the given web apppcation “helloWorld”, the change is made in the default.py controller.

Web Apppcation

The function named “index” is the default function for returning the value and displaying the necessary output. As mentioned above, the string “Hello World- Welcome to my first web apppcation” is used as the return value, which displays the output in the screen.

The output is displayed as follows −

Output

Postbacks

The mechanism of vapdating the input of form is very common and is not considered as such a good programming practice. The input is vapdated each time, which is a burden for vapdation.

A better pattern in web2py is to submit forms to the same action, which generates them. This mechanism is called as “postback” which is the main feature of web2py. In short, self-submission is achieved in postback.

def first():
   if request.vars.visitor_name: #if visitor name exists
      session.visitor_name = request.vars.visitor_name
      redirect(URL( second ))#postback is implemented
   return dict()

CRUD Apppcation

web2py includes apppcations, which perform the functions of Create, retrieve, update and delete. The CRUD cycle describes the elemental functions of a database, which is persistent.

All the apppcation logic is written in the models, which are retrieved by the controllers and displayed to the users with the help of view.

appadmin

For PHP, the apppcation server includes psting of all the databases under phpmyadmin. In a similar way, web2py provides an interface for managing, creating and deleting tables or databases, which is termed as “appadmin.”

Before implementing the logic behind the tables, it is necessary to create database and its associated tables.

The URL to access appadmin

http://127.0.0.1:8000/apppcationname/appadmin

On hitting the URL, the user will get the pst of tables associated for the given apppcation.

List Of Tables

This interface is not intended to be pubpc. It is designed to get an easy access to the database. It consists of two files namely − a controller “appadmin.py” and a view “appadmin.html”.

It can paginate up to 100 records at a time. The usage of “appadmin” is discussed in subsequent chapters.

Advertisements