You are on page 1of 10

Web2py

Controllers, actions, and views


Controllers, actions, and views
• In Web2py, controllers, actions, and views work together to handle
requests and generate responses for web applications.
• Controllers are Python modules that define functions to handle requests.
Each function in a controller is called an action, and it typically performs
some data processing and prepares data to be displayed in a view.
• Actions are functions that receive and return parameters. When a user
requests a URL, Web2py maps the URL to a specific controller and
action, and the action is executed.
• Once the action has processed any necessary data, it is prepared to be
displayed in a view.
Controllers, actions, and views
• Views are HTML templates that define the structure and layout of the
response. Views can include placeholders for data that is dynamically
generated by the controller or action.
• In Web2py, views can be written using different templating languages,
such as HTML, CSS, and JavaScript, and can be customized for
different devices and user interfaces.
Separation of Concerns
• The separation of concerns between controllers, actions, and views
allows for greater flexibility and modularity in web application
development.
• Changes can be made to individual components without affecting the
rest of the application, making it easier to maintain and update code.
Mapping URLs
• By default, Web2py maps simple URL requests to a controller, action,
and view based on the URL path.
• For example, a request for http://localhost:8000/myapp/default/index
is mapped to the default.py controller, index action, and
default/index.html view.
• The default mapping mechanism is case-insensitive for controller,
action, and view names.
Mapping URLs
• If a requested controller, action, or view is not found, Web2py raises
an HTTP 404 error.
• Web2py has a default controller and action that are used when a URL
path is not specified.
• By default, the default.py controller and index action are used for such
requests.
• The default mapping mechanism in Web2py is designed to make it
easy for developers to build simple web applications quickly, while
still providing the flexibility to customize the mapping for more
complex scenarios.
controllers, actions, and views interaction
1.A user requests a URL, such as /myapp/default/index.
2.Web2py maps the URL to the default.py controller and the index
action.
3.The index action retrieves data from a database or other data source
and prepares it to be displayed in the view.
4.The prepared data is passed to a view, such as default/index.html,
which defines the HTML structure and layout of the response.
5.The view renders the response, including any dynamically generated
data, and returns it to the user's browser.
Example
• Controller (default.py): which contains
• Action (index function):

• This action sets a variable name to the string 'Web2py'.


• It returns a dictionary containing name as a key and its value as the value.
Example
• View (default/index.html):
Example
How does it work:
• The controller has a single action named index.
• The index action sets the variable name to the string 'Web2py'.
• It then returns a dictionary with the key name and its value.
• The view default/index.html uses {{=name}} to access the value of the
name variable and include it in the HTML response.
• When the user visits the URL (localhost:8080//myapp/default/index) that
maps to this controller and action, Web2py will execute the index action,
prepare the data, and render the view with the dynamically generated data.

You might also like