If you benefit from web2py hope you feel encouraged to pay it forward by contributing back to society in whatever form you choose!

in model

print("

      db.define_table('person',    
          Field('name'))

");

in controller

insert...........................................

print("

 def index():
      return dict()

 def data():
      listado = []
      if request.vars.q:            
         db.person.insert(name = request.vars.q)      
         for elem in db(db.person).select():
               listado += elem.name + "<br/>" 
      else:       
          for elem in db(db.person).select():
                listado += elem.name + "<br/>"    
      return listado

");

delete..................................................................

print("

  def index():
      return dict()

  def data():
       listado = []    
       if request.vars.q:       
            db(db.person.name == request.vars.q).delete()
            if(db(db.person.id > 0).count()==0):
                   return 'no data'
            else:
                pass     
            for elem in db(db.person).select():
                    listado += elem.name + "<br/>" 
       else:              
             for elem in db(db.person).select():
                       listado += elem.name + "<br/>"    
       return listado

");

index.html

print("

     <script type='text/javascript'>
     $(document).ready(function(){
     cargarLista();        
     function cargarLista(){
     $('#target').load('data');
    }
     })
  </script>

   <INPUT type="text"  name="q"  />
   <INPUT type="button" value="submit" onclick="ajax('{{=URL('data')}}',['q'],'target');"/><br/>

    <div id='target'> </div>

");

Gmail example

print("

  db.define_table('gmail',   
         Field('correo')) 
   ................................................................................................................
   index.html:

   {{extend 'layout.html'}}
     <INPUT type="button" value="suprimir" onclick="ajax('{{=URL('data')}}',['hijos'],'target');"/> <br/><br/>

   <span id='target'>
     {{if len(lista):}}
     {{for row in lista:}}
           {{=INPUT(_type='checkbox', _name='hijos', _value=row.id)}}
         <span style='margin-left:0%'>{{=row.correo}}</span><br/>
         {{pass}}
    {{else:}}
           No data
    {{pass}}
    </span> 
   ...................................................................................
   default.py
   def index():   
           lista = db(db.gmail).select()   
   return dict(lista=lista)

    def data():   
          listado = []      
          if request.vars.hijos:
                 listacheck = request.vars.hijos
                 if not isinstance(listacheck,list):
                        listacheck=[listacheck,]         

                 for elem in listacheck:                    
                        db(db.gmail.id== elem).delete()

                 if(db(db.gmail.id > 0).count()==0):
                       return 'No data'
                 else:
                      pass   

                 for row in db(db.gmail).select():
                       listado.append(INPUT(_type='checkbox',_name='hijos',_value=row.id))
                       listado.append(SPAN(row.correo,_style='margin-left:0.5%'))         
                       listado.append(BR())

                 a = DIV(listado)

          return a

");

Related slices

Comments (3)


Hosting graciously provided by:
Python Anywhere