Table of contents
1.
Introduction
2.
Default Page Layout
2.1.
The Features of the Default Layout
3.
Customizing the Default Page Layout
4.
Mobile Development
5.
Frequently Asked Questions
5.1.
Which is better, web2py or Django?
5.2.
What is web2py used for?
5.3.
Does web2py support Python 3?
5.4.
What is the web2py framework?
5.5.
Is web2py an MVC?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Default Page Layout and Mobile Development in Web2py

Author soham Medewar
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Python is used to create the open-source Web2py framework for web applications. Dynamic web content programming in python is made possible via Web2py. Web2py is made to make laborious web development jobs easier, such as creating web forms from scratch, while a web developer can still do it if necessary.

Web2py cover

In this article, we will be learning the default page layout and mobile development in tweb2py.

Default Page Layout

Despite being stripped of certain optional components, the "views/layout.html" that comes with the Web2py scaffolding application welcome has the same structure:

Code for default page layout

<!DOCTYPE html>
<head>
  <meta charset="utf-8" />
  <title>{{=response.title or request.application}}</title>
  ...
  <script src="{{=URL('static', 'js/modernizr.custom.js')}}"></script>

  {{
  response.files.append(URL('static', 'css/web2py.css'))
  response.files.append(URL('static', 'css/bootstrap.min.css'))
  response.files.append(URL('static', 'css/bootstrap-responsive.min.css'))
  response.files.append(URL('static', 'css/web2py_bootstrap.css'))
  }}

  {{include 'web2py_ajax.html'}}

  {{
  # using sidebars need to know what sidebar you want to use
  left_sidebar_enabled = globals().get('left_sidebar_enabled', False)
  right_sidebar_enabled = globals().get('right_sidebar_enabled', False)
  middle_columns = {0:'span12', 1:'span9', 2:'span6'}[
    (left_sidebar_enabled and 1 or 0)+(right_sidebar_enabled and 1 or 0)]
  }}

  {{block head}}{{end}}
</head>

<body>
  <!-- Navbar -->
  <div class="navbar navbar-inverse navbar-fixed-top">
    <div class="flash">{{=response.flash or ''}}</div>
    <div class="navbar-inner">
      <div class="container">
        {{=response.logo or ''}}
        <ul id="navbar" class="nav pull-right">
          {{='auth' in globals() and auth.navbar(mode="dropdown") or ''}}
        </ul>
        <div class="nav-collapse">
          {{if response.menu:}}
          {{=MENU(response.menu)}}
          {{pass}}
        </div><!--/.nav-collapse -->
      </div>
    </div>
  </div><!--/top navbar -->

  <div class="container">
    <!-- Masthead -->
    <header class="mastheader row" id="header">
        <div class="span12">
            <div class="page-header">
                <h1>
                    {{=response.title or request.application}}
                    <small>{{=response.subtitle or ''}}</small>
                </h1>
            </div>
        </div>
    </header>

    <section id="main" class="main row">
        {{if left_sidebar_enabled:}}
        <div class="span3 left-sidebar">
            {{block left_sidebar}}
            <h3>Left Sidebar</h3>
            <p></p>
            {{end}}
        </div>
        {{pass}}

        <div class="{{=middle_columns}}">
            {{block center}}
            {{include}}
            {{end}}
        </div>

        {{if right_sidebar_enabled:}}
        <div class="span3">
            {{block right_sidebar}}
            <h3>Right Sidebar</h3>
            <p></p>
            {{end}}
        </div>
        {{pass}}
    </section><!--/main-->

    <!-- Footer -->
    <div class="row">
        <footer class="footer span12" id="footer">
            <div class="footer-content">
                {{block footer}} <!-- this is default footer -->
                ...
                {{end}}
            </div>
        </footer>
    </div>

  </div> <!-- /container -->

  <!-- Javascript -->
  <!-- Jacascript code is placed at the end to load the page faster -->
  <script src="{{=URL('static', 'js/bootstrap.min.js')}}"></script>
  <script src="{{=URL('static', 'js/web2py_bootstrap.js')}}"></script>
  {{if response.google_analytics_id:}}
    <script src="{{=URL('static', 'js/analytics.js')}}"></script>
    <script type="text/javascript">
    analytics.initialize({
      'Google Analytics':{trackingId:'{{=response.google_analytics_id}}'}
    });</script>
  {{pass}}
</body>
</html>

The Features of the Default Layout

  • It uses the "modernizr" [modernizr] package and is built in HTML5 to support older browsers. In order to keep the layout concise, certain additional conditional statements that are required by IE are included.
     
  • The response.subtitle and response.title attributes, which may be configured in a model or controller, are both displayed. It uses the application name as the title if they are not set.
     
  • The header contains the web2py ajax.html file, which produced all of the link and script import declarations.
     
  • It adapts columns to fit small displays and uses a modified version of Twitter Bootstrap for flexible layouts that function on mobile devices.
     
  • To connect to Google Analytics, it makes use of "analytics.js."
     
  • Depending on the situation, the {{=auth.navbar(...)}} shows a greeting to the current user and links to the auth functions like login, register, logout, change password, etc. As with any other helper factory, auth.navbar's output can be modified. It is used in an expression to check for the definition of auth; if auth is not defined, the expression evaluates to ".
     
  • The menu structure is displayed as <ul>...</ul> using the {{=MENU(response.menu)}} function.
     
  • When the page is rendered, "include" is substituted with the information from the extending view.
     
  • It employs the following classes: page header, main, and footer. By default, it uses a conditional three-column layout (the extending views have the ability to disable the left and right sidebars).
     
  • The blocks head, left sidebar, center, right sidebar, and footer are all present.

We can also customize the sidebars in the default page layout as follows:

{{left_sidebar_enabled=True}}
{{extend 'layout.html'}}

Text goes in the middle of the page.

{{block left_sidebar}}
This text goes in the sidebar.
{{end}}

Customizing the Default Page Layout

customising the default

As the welcome application is based on Twitter Bootstrap, which is fully documented and supports themes, customizing the basic layout without modifying is simple. Four static files in web2py that are important to style are:

  • ​​"css/web2py.css" includes styles particular to web2py.
  • The Twitter Bootstrap CSS style [bootstrap] is found in "css/bootstrap.min.css,"
  • "css/web2py bootstrap.css" replaces various Bootstrap styles to meet web2py requirements.
  • "js/bootstrap.min.js" contains the libraries for menu effects, modals, and panels.

Try adding the following code to the layout.html header to modify the colors and background images:

Code 👨‍💻

<style>
body { background: url('images/background.png') repeat-x #3A3A3A; }
a { color: #349C01; }
.page-header h1 { color: #349C01; }
.page-header h2 { color: white; font-style: italic; font-size: 14px;}
.statusbar { background: #333333; border-bottom: 5px #349C01 solid; }
.statusbar a { color: white; }
.footer { border-top: 5px #349C01 solid; }
</style>


Of course, you can substitute your own files for "layout.html" and "web2py.css" entirely.

Mobile Development

Mobile development

Even though the default layout.html is mobile-friendly, it is occasionally necessary to employ alternative views when a page is accessed via a mobile device.

Web2py contains the @mobilize decorator to facilitate desktop and mobile device development. This decorator is used on activities that need to have both a desktop and mobile version. Here is an illustration of that:

from gluon.contrib.user_agent_parser import mobilize
@mobilize
def index():
    return dict()


Keep in mind that before using the decorator in a controller, it must first be imported. Web2py will display the returned dictionary when the "index" function is used from a standard browser (desktop computer) using the view "[controller]/index.html." However, "[controller]/index.mobile.html" will render the dictionary when it is called from a mobile device. Take note of the "mobile.html" extension for mobile viewing.

As an alternative, you can use the logic outlined below to make every view mobile-friendly:

if request.user_agent().is_mobile:
    response.view.replace('.html', '.mobile.html')


Although it is up to the developer to create the "*.mobile.html" views, we strongly advise using the "jQuery Mobile" plugin because it makes the process quite simple.

Frequently Asked Questions

Which is better, web2py or Django?

Due to its smaller size, simpler learning curve, and lack of project-level configuration files, web2py differs from Django. Compared to PHP-based frameworks and Java-based frameworks, web2py has a significantly clearer syntax. This makes applications easier to comprehend, maintain, and create.

What is web2py used for?

Python dynamic web content programming is made possible via Web2py. Web2py is made to make laborious web development jobs easier, such as creating web forms from scratch, while a web developer can still do it if necessary.

Does web2py support Python 3?

web2py runs with CPython (the C implementation) and PyPy (Python written in Python) on Python 2.7 and Python 3.

What is the web2py framework?

Web2py, which is written in Python and programmable in Python, is described as a free, open-source online framework for agile development that involves database-driven web applications. It is a full-stack framework.

Is web2py an MVC?

The Model View Controller (MVC) pattern is one of the best practices for software engineering that web2py is intended to help web developers adhere to.

Conclusion

In this article, we have learned about the Default page layout and Mobile development in web2py.

If you want to learn more, check out our articles, Linked lists in pythonDjango Interview QuestionsFull stack web development, and 7 Best Web Development Project Ideas 2021.

Do upvote our blogs if you find them helpful and engaging!

Happy Learning, Ninjas!

Thankyou image
Live masterclass