Published in 20:08 of 09/11/2012 by

Published in 20:08 of 09/11/2012

←Home

App news reading (portuguese)

models/db.py

db = DAL("sqlite://news.sqlite")
db.define_table("noticias",
    Field("titulo"),
    Field("texto", "text"),
    Field("data", "datetime")
    )

controllers/default.py

def escrever():
    form = SQLFORM(db.noticias)
    if form.process().accepted:
        redirect(URL("listar"))
    return dict(form=form)

def listar():
    noticias = db(db.noticias).select(orderby=~db.noticias.data)
    return dict(noticias=noticias)

def ler_noticia():
    id_noticia = request.args(0) or redirect(URL("listar"))
    # caso não passe um id retorna para /listar
    noticia = db.noticias[id_noticia]
    return dict(noticia=noticia)

views/default/listar.html

{{extend "layout.html"}}

<ul>
{{for noticia in noticias:}}
<li> 
    <a href="{{=URL("default", "ler_noticia", args=noticia.id)}}"> 
    {{=noticia.titulo}} 
    </a>
 </li>
{{pass}}
</ul>

views/default/ler_noticia.html

{{extend "layout.html"}}

<h1>
    <a href="{{=URL("default", "ler_noticia", args=noticia.id)}}">
        {{=noticia.titulo}} 
    </a>
</h1>
<p>
{{=XML(noticia.texto)}}
</p>

views/default/escrever.html

{{extend "layout.html"}}

<h1> escreva uma noticia</h1>
{{=form}}

WEb2py 2.0

  • Programmatically check if Django South has migrations to run in django · 23:48 of 01/03/2013
  • Lazy DAL - Attempt 3 - Pbreit in web2py · 14:27 of 08/15/2012
  • Open links which points outside your own site in a new window in python · 00:43 of 08/14/2012

  • comments powered by Disqus Go Top