20
mini curso web2py @lucadavila

Web2py

  • Upload
    lucas-d

  • View
    1.510

  • Download
    2

Embed Size (px)

DESCRIPTION

Slides sobre framework web2py utilizados em um mini curso no unisul techday em Tubarão, SC.

Citation preview

Page 1: Web2py

mini curso

web2py@lucadavila

Page 2: Web2py

framework web python

Page 3: Web2py

# opensource

# seguro

# model view controller

# windows, mac, unix/linux, GAE

# SQLite, PostgreSQL, MySQL, Oracle, GAE...

# apache, lighttpd, cherokee, *mod_python ...

# compatibilidade reversa

# curva aprendizado pequena

# interface administrativa web

Page 4: Web2py

/models/db.py

Page 5: Web2py

#database definition

db = DAL('sqlite://storage.sqlite')

Page 6: Web2py

#model definition

Person = db.define_table('persons',

Field('name', label='Your name'),

Field('email'),

)

#validators

Person.name.requires = IS_NOT_EMPTY()

Person.email.requires = IS_EMPTY_OR(IS_EMAIL())

Page 7: Web2py

#insert (without validating)

Person.insert(name="Lucas D'Avila", email="[email protected]")

Person.insert(name="", email="foo")

#validate and insert

Person.validate_and_insert(name="", email="foo")

<Row {'errors': <Row {'name': <lazyT 'enter a value'>,

'email': <lazyT 'enter a valid email address'>}>, 'id': None}>

Page 8: Web2py

#save changes

db.commit()

#undo changes

db.rollback()

Page 9: Web2py

#count

db(Person.id > 0).count()

#select

persons = db(Person.id > 0).select()

person_by_id = Person(1)

Page 10: Web2py

#update set

db(Person.id > 0).update(name="Luke")

#update record

Person(1).update_record(name="Lucas")

Page 11: Web2py

#delete set

db(Person.id > 0).delete()

#delete record

Person(1).delete_record()

#truncate table (restart id sequence)

Person.truncate()

Page 12: Web2py

/controllers/persons.py

Page 13: Web2py

#action returning a string

def say_hello():

return dict(some_var = "Hello world!")

#action returning a set of database records

def persons() :

persons = db(Person.id > 0).select()

return locals()

Page 14: Web2py

/views/persons/say_hello.html

Page 15: Web2py

#view

<h1>web2py views</h1>

<p>Controller says: {{=some_var}}</p>

Page 16: Web2py

#url mappinghttp://hostname /app/controller/action/arg/arg2/...?var=lucas

Page 17: Web2py

#dispatching

Page 20: Web2py

Obrigado!