Web2py Official Document Translation 01

Source: Internet
Author: User
Tags db2 ibm db2 informix table definition web database web2py

The first chapter: Introduction

Introduced

Web2py (web2py) is a free open source web framework for Agile Development of secure database-driven Web applications, Python (python) written in Python and programmable. WEB2PY is a complete framework, meaning that it contains all the components you need to build a fully functional Web application.

Web2py is designed to guide Web developers to follow good software engineering practices, such as using model-view-controller (MVC) patterns. Web2py separates data representations (models) from the data display (view) and from application logic and Workflow (Controller). WEB2PY provides a library to help developers design, implement, and test these three sections separately to let them work together.

Web2py safe. This means that it automatically resolves many issues that can lead to security breaches by following good practices. For example, it verifies all inputs (preventing injections), escapes all outputs (prevents cross-site scripting), and renames uploaded files (to prevent directory traversal attacks). WEB2PY is responsible for key security issues, so developers are less likely to introduce vulnerabilities.

Web2py includes a Database abstraction layer (DAL) dynamic SQL (SQL-W) so you write that developers don't need to. The wood bean knows how to generate SQL Transparent SQLite sqlite,mysql (MySQL), Postgresql[postgres], the software [that], Fire bird Fire Bird, Oracle (Oracle), IBM DB2 (DB2), Informix ( Informix) and Angel (INGRESDB).

Google's Wood beans can also generate function call data stored on Google App Engine (gae) running. Experiment we support multiple databases and new constantly add to the Tao. Please check the WEB2PY website and the mailing list of the nearest adapter. Once you have defined one or more database tables, Web2py automatically generates a fully functional Web database management interface to access the database and tables.

Web2py differs from other web frameworks in that it is the only framework that fully accepts Web 2.0 mode, and the network is a computer. In fact, Web2py does not need to be installed or configured, it runs in any architecture, can run Python (windows,windows ce,mac OS X,ios and Unix/linux), and develops, Applications in the deployment and maintenance phase can be through a local or remote Web interface. The web2py runs with CPython (c implementation) and PyPy (Python-written), Python versions 2.5,2.6 and 2.7.

WEB2PY provides ticketing system error events. If an error occurs, a ticket is sent to the user, and the administrator's error is logged.

Web2py is open source and released under license version 3 LGPL.

Web2py is another important feature of ours, and its developers are committed to maintaining backwards compatibility in future versions. We have done so since the first release of Web2py last October, 2007. Added new features and bugs have been fixed, but if a program has used WEB2PY 1.0, today's plan will work better.

Here are some examples of web2py statements that illustrate the power and simplicity of it. The following code:

Db. define_table (' Person ', field ("Name"), field ("Image", "Upload"))

Create a database table called "People" two fields: "First Name", a string; and "image", need to upload (actual image). If the table already exists but does not match this definition, it is appropriate to change.

Given the table definition above, the following code:

form = Sqlform (Db.person). Process ()

Create an Insert form for this table that allows users to upload images. It also validates the submitted form, renames the uploaded image in a secure manner, stores the image in a file, inserts the corresponding record in the database, prevents double yielding, and eventually modifies the form itself by adding an error message if the user submits the data without verifying it.

This code embeds a full working wiki tag, search, Tag cloud, permissions, media attachment, and oembed support:

def index (): Return Auth.wiki ()

The following code:

@auth. Requires_permission ("read", "person")

def f (): ....

Prevent visitors from accessing member functions f unless the guest is a group of members whose permissions are recorded in the "read" Table of "people". If the user is not logged in, the visitor is directed to a login page (provided via web2py by default).

Web2py also supports components, i.e. the behavior can be loaded in a view and interact with the visitor through Ajax without cleaning rod and the barrel hitting the entire page. This is an application that allows modular design through a load helper; it is discussed in chapter three in the context of the wiki, in some detail, in the last chapter of the book.

This sixth edition of the book describes the Web2py 2.4.1 and later versions.

Principle

Python programming usually follows these basic principles:

Don't repeat yourself (dry).

There should be only one way to do things.

Explicit is better than implicit.

Web2py fully incorporates the first two principles, forcing developers to use good software engineering practices to reduce duplication of code. WEB2PY guides developers through almost all of the tasks common in Web application development (creating and processing forms, managing meetings, cookies, errors, and so on).

Web2py differs from other frameworks to the third principle, sometimes with two of other conflicts. In particular, WEB2PY does not import user applications, but they are executed in a predefined context. This situation exposes the Python keyword, as well as the WEB2PY keyword.

This may appear some magic, but it should not. Simply, in practice, some modules have not been imported as you do. Web2py tries to avoid annoying other framework features, forcing developers to import the same on top of each model and controller module.

Web2py, by importing its own modules, saves time and prevents errors, so in the "Don't repeat Yourself" spirit and "there should be only one way to do things."

If developers want to use other Python modules or third-party modules, these modules must be explicitly imported, such as other Python programs.

Web Framework

At the most basic level, the Web application is accessed by a set of programs (or functions) when the corresponding URL is executed. The output of the program is returned to the visitor and browser rendered.

The purpose of the web framework is to enable developers to quickly build new applications, easily and without errors. This is done by providing APIs and tools that reduce and simplify the number of encodings required.

Two classic ways to develop a Web application are:

Generates HTML programmatically (Html-w][html-o].

Embed the code in an HTML page.

The first model is later, for example, through early CGI scripts. The second model, for example, is through PHP (PHP code, a C language), ASP in Visual Basic (code), and JSP (Java code).

This is an example of a PHP program that, when executed, retrieves data from the database and returns an HTML page that displays the selected records:

Body < HTML > < > < h1 >

Mysql_connect (host, user name, password);

@mysql_select_db (database) or death ("Unable to select database");

Query USD = "SELECT * from Contact";

USD $ result = mysql_query (query);

Mysql_close ();

$ i = 0;

USD ($ me < Mysql_numrows (result)) {

The result (name = mysql_result US Dollar, dollar me, "name");

$ phone = Mysql_result (therefore, USD I, "phone");

echo "< b > USD name </b > < br > Tel: Phone USD < br/> < br/> < HR/> < br/>";

dollars i + +;

}

Body? > < >

The problem with this approach is that the code is embedded in HTML, but the same code also needs to generate additional HTML and generate SQL statements to query the database, involving multiple layers of applications that make it difficult to read and maintain. The situation of AJAX applications is worse, and the complexity increases the number of pages (files) of the application.

The above example can express the function in web2py two lines of Python code:

def index ():

Returned HTML (body (H1 (record), db (). Select (Db.contacts.ALL)))

In this simple example, the HTML page structure represents HTML programming, the body, and the H1 object, querying the DB's Select command, and finally, everything is serialized into HTML. Note that DB is not a keyword but a user-defined variable. We will use this name to always refer to a database connection to avoid confusion.

Web frameworks are typically divided into two types: the "sticky" framework is composed of several third-party components that are assembled (glued together). A "complete framework is created by creating components that are specifically tightly integrated and work together.

The WEB2PY is a complete framework. Almost all of the components are zero-based and are designed to work together, but they are just like functions outside the WEB2PY framework. For example, the database abstraction layer (DAL) or you can use the template language independent of the WEB2PY framework by importing the glue. Wood beans or gels. To the template of your own Python application. The glue sub is the name of the Web2py module, which contains the system library. Some web2py libraries, such as architectural and processing forms from database tables, web2py depend on other parts. Web2py can also use third-party Python libraries, including other template languages and wood beans, but they will not be as tightly integrated as the original components.

Model-View-controller

WEB2PY encourages developers to separate data representations (models), data display (views), and application workflows (controllers). Let's consider the previous example to see how to build a web2py application. This is an example of the web2py MVC editing Interface:

Web2py Official Document Translation 01

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.