SlideShare a Scribd company logo
Web2py Tutorial

    This presentation is a summary of the
 documentation to create a simple database
 driven application using the web2py python
framework. The documentation can be found
                     here.
Download and Run




Download the framework from here. Go to the
directory where you downloaded the
application, and run python web2py.py.
Choose an Admin Password.
Login to the Admin Interface
Create an Application
Database Modeling
Database Model Implementation

''' We create a table notes, with the columns title, description, author,
subject and a publication date. We also mention that the fields title, description,
author are required, and that the publication date will be automatically assigned the current time.'''

db.define_table('notes',
          Field('title', length=200, required=True, requires=IS_NOT_EMPTY()),
          Field('description', length=2000, required=True, requires=IS_NOT_EMPTY()),
          Field('author', db.auth_user, required=True, requires=IS_NOT_EMPTY()),
          Field('subject', length=20,requires=IS_IN_SET(('english', 'philosophy', 'metaphysics'))),
          Field('pub_date', 'datetime', default=request.now, writable=False)
          )
We need functions that handle the
request from user.
Function Definition to handle CRUD
operations. (index)
def index():

  ''' Makes a db query to select all the notes,
  orders the notes by the publication date and
  returns a dictionary to the template, containing
  all the notes.'''

  response.flash = "Welcome to the index view!"
  notes = db(db.notes).select(orderby=db.notes.pub_date)
  return dict(notes=notes)
Function Definition contd.. (create)
def create():

  ''' Generates a form corresponding to the model and
      renders it, if the form sends some data, the function
      validates the data and saves the data in the database.'''

  response.flash = "This is the create page"
  form=SQLFORM(db.notes)
  if form.process().accepted:
     response.flash = 'form accepted'
  elif form.errors:
     response.flash = 'form has errors'
  else:
      response.flash = 'please fill the form'
  return dict(form=form)
Function Definitions contd.. (edit)
def edit():


  ''' The function pre-populates the data from the note instance
     that has been requested to be edited and renders it,
     once client sends in some data, it saves it in the database.'''


  note = db.notes(request.args(0)) or redirect(URL('error'))
  form=SQLFORM(db.notes, note, deletable = True)
  if form.validate():
     if form.deleted:
          db(db.notes.id==note.id).delete()
          redirect(URL('index'))
     else:
          note.update_record(**dict(form.vars))
          response.flash = 'records changed'
  else:
     response.flash = 'Something went wrong!'
  return dict(form=form)
We need to create HTML templates
2 basic templates would do the job!
The previous slide shows how to create a
template. We need to create a template to
show all the content (index.html) and a
template to create/edit the content(create.html)

The files can be found here.
Its Done!!
Its time to reap the fruits of the hard work
web2py just did for us :)

The source code is available here.

You can share it if you liked it, in case of doubts
send a mail to: contact@fruiapps.com.

More Related Content

ODP
φωτοχημικο νεφος
PDF
Regulamento volei
PPTX
Aula 13 - Práticas Corporais de Aventura Urbanas ou da Natureza.pptx
PPTX
Destrinchando o livro "Richthofen - o assassinato dos pais de Suzane"
PDF
Logística reversa philips
PPTX
AΘΛΗΤΙΣΜΟΣ ΚΑΙ ΒΙΑ
PDF
Ρύζι_Τσάμη
PPTX
SLIDE VIOLENCIA CONTRA A MULHER.pptx lima campos
φωτοχημικο νεφος
Regulamento volei
Aula 13 - Práticas Corporais de Aventura Urbanas ou da Natureza.pptx
Destrinchando o livro "Richthofen - o assassinato dos pais de Suzane"
Logística reversa philips
AΘΛΗΤΙΣΜΟΣ ΚΑΙ ΒΙΑ
Ρύζι_Τσάμη
SLIDE VIOLENCIA CONTRA A MULHER.pptx lima campos

Viewers also liked (11)

PDF
web2py:Web development like a boss
PDF
Web2py Code Lab
PDF
Using web2py's DAL in other projects or frameworks
PPTX
Mule esb – connecting to ms sql db
PDF
Plantillas para la creación de material docente accesible con herramientas of...
PDF
LaTeX sin dolor.
PDF
5-Beamer: Creación de presentaciones con LaTeX
PDF
Desenvolvendo mvp com python
PDF
Web2py
PDF
Gestión de datos de investigación: trabajo cooperativo en las bibliotecas de ...
PDF
Latex - Creación de documentos cientificos
web2py:Web development like a boss
Web2py Code Lab
Using web2py's DAL in other projects or frameworks
Mule esb – connecting to ms sql db
Plantillas para la creación de material docente accesible con herramientas of...
LaTeX sin dolor.
5-Beamer: Creación de presentaciones con LaTeX
Desenvolvendo mvp com python
Web2py
Gestión de datos de investigación: trabajo cooperativo en las bibliotecas de ...
Latex - Creación de documentos cientificos
Ad

Similar to Web2py tutorial to create db driven application. (20)

PDF
Rapid Prototyping with PEAR
PDF
PofEAA and SQLAlchemy
PDF
The Magic Revealed: Four Real-World Examples of Using the Client Object Model...
PPT
Developing node-mdb: a Node.js - based clone of SimpleDB
PPTX
Jquery dojo slides
PPTX
Writing HTML5 Web Apps using Backbone.js and GAE
PDF
Laravel 8 export data as excel file with example
PDF
SproutCore and the Future of Web Apps
PPTX
AngularJS Internal
PPTX
AngularJS Architecture
PDF
Taking Web Apps Offline
PDF
ASP.NET Core MVC with EF Core code first
PDF
Web internship Yii Framework
ZIP
What's new in the Drupal 7 API?
PPTX
dotNet Miami - June 21, 2012: Richie Rump: Entity Framework: Code First and M...
PDF
Tips and tricks for building api heavy ruby on rails applications
PPTX
Entity Framework: Code First and Magic Unicorns
PDF
Scalable web application architecture
ODP
Slickdemo
PDF
Ejb3 Struts Tutorial En
Rapid Prototyping with PEAR
PofEAA and SQLAlchemy
The Magic Revealed: Four Real-World Examples of Using the Client Object Model...
Developing node-mdb: a Node.js - based clone of SimpleDB
Jquery dojo slides
Writing HTML5 Web Apps using Backbone.js and GAE
Laravel 8 export data as excel file with example
SproutCore and the Future of Web Apps
AngularJS Internal
AngularJS Architecture
Taking Web Apps Offline
ASP.NET Core MVC with EF Core code first
Web internship Yii Framework
What's new in the Drupal 7 API?
dotNet Miami - June 21, 2012: Richie Rump: Entity Framework: Code First and M...
Tips and tricks for building api heavy ruby on rails applications
Entity Framework: Code First and Magic Unicorns
Scalable web application architecture
Slickdemo
Ejb3 Struts Tutorial En
Ad

Recently uploaded (20)

PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
Cloud computing and distributed systems.
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
Big Data Technologies - Introduction.pptx
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PPTX
MYSQL Presentation for SQL database connectivity
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
KodekX | Application Modernization Development
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Electronic commerce courselecture one. Pdf
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Unlocking AI with Model Context Protocol (MCP)
Cloud computing and distributed systems.
Mobile App Security Testing_ A Comprehensive Guide.pdf
MIND Revenue Release Quarter 2 2025 Press Release
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Reach Out and Touch Someone: Haptics and Empathic Computing
Big Data Technologies - Introduction.pptx
20250228 LYD VKU AI Blended-Learning.pptx
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Per capita expenditure prediction using model stacking based on satellite ima...
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
MYSQL Presentation for SQL database connectivity
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
KodekX | Application Modernization Development
Chapter 3 Spatial Domain Image Processing.pdf
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
The Rise and Fall of 3GPP – Time for a Sabbatical?
Electronic commerce courselecture one. Pdf

Web2py tutorial to create db driven application.

  • 1. Web2py Tutorial This presentation is a summary of the documentation to create a simple database driven application using the web2py python framework. The documentation can be found here.
  • 2. Download and Run Download the framework from here. Go to the directory where you downloaded the application, and run python web2py.py.
  • 3. Choose an Admin Password.
  • 4. Login to the Admin Interface
  • 7. Database Model Implementation ''' We create a table notes, with the columns title, description, author, subject and a publication date. We also mention that the fields title, description, author are required, and that the publication date will be automatically assigned the current time.''' db.define_table('notes', Field('title', length=200, required=True, requires=IS_NOT_EMPTY()), Field('description', length=2000, required=True, requires=IS_NOT_EMPTY()), Field('author', db.auth_user, required=True, requires=IS_NOT_EMPTY()), Field('subject', length=20,requires=IS_IN_SET(('english', 'philosophy', 'metaphysics'))), Field('pub_date', 'datetime', default=request.now, writable=False) )
  • 8. We need functions that handle the request from user.
  • 9. Function Definition to handle CRUD operations. (index) def index(): ''' Makes a db query to select all the notes, orders the notes by the publication date and returns a dictionary to the template, containing all the notes.''' response.flash = "Welcome to the index view!" notes = db(db.notes).select(orderby=db.notes.pub_date) return dict(notes=notes)
  • 10. Function Definition contd.. (create) def create(): ''' Generates a form corresponding to the model and renders it, if the form sends some data, the function validates the data and saves the data in the database.''' response.flash = "This is the create page" form=SQLFORM(db.notes) if form.process().accepted: response.flash = 'form accepted' elif form.errors: response.flash = 'form has errors' else: response.flash = 'please fill the form' return dict(form=form)
  • 11. Function Definitions contd.. (edit) def edit(): ''' The function pre-populates the data from the note instance that has been requested to be edited and renders it, once client sends in some data, it saves it in the database.''' note = db.notes(request.args(0)) or redirect(URL('error')) form=SQLFORM(db.notes, note, deletable = True) if form.validate(): if form.deleted: db(db.notes.id==note.id).delete() redirect(URL('index')) else: note.update_record(**dict(form.vars)) response.flash = 'records changed' else: response.flash = 'Something went wrong!' return dict(form=form)
  • 12. We need to create HTML templates
  • 13. 2 basic templates would do the job! The previous slide shows how to create a template. We need to create a template to show all the content (index.html) and a template to create/edit the content(create.html) The files can be found here.
  • 14. Its Done!! Its time to reap the fruits of the hard work web2py just did for us :) The source code is available here. You can share it if you liked it, in case of doubts send a mail to: contact@fruiapps.com.