simple db app

23 views
Skip to first unread message

Rick

unread,
Dec 13, 2010, 6:55:19 PM12/13/10
to web2py-users
Hi,

I'm trying to make a data base for storing names and addresses, but
since I'm a n00b then I can't get it working. I've no idea what's
wrong. The problem is that the input function doesn't redirect to the
show function and that the show function doesn't show any data. Here's
the code:

the controller file:
def show():
id=request.vars.id
record=db(db.measure.id==id).select()
return dict(record=record)

def input():
form = SQLFORM(db.addresses)
if form.accepts(request.vars, session):
redirect(URL(r=request, f='show'))
return dict(form=form)

**********************

and the model file table:

db.define_table('addresses',
Field('person'),
Field('adress'))

**********************

input.html
{{extend 'layout.html'}}
{{=form}}


**********************

show.html:
{{extend 'layout.html'}}
<h1>Addresses</h1>
{{for record in record:}}
{{=record.name}} : {{=record.address}}<br>
{{pass}}

**********************

Thanks in advance for help!

Rick Hultgren

unread,
Dec 13, 2010, 6:58:15 PM12/13/10
to web2py-users

pbreit

unread,
Dec 13, 2010, 8:51:01 PM12/13/10
to web...@googlegroups.com
I would envision something more like this (not tested):

== db.py ==
db.define_table('address', 
    Field('person'), 
    Field('address'))

== default.py ==
def index(): 
    records = db().select(db.address.ALL, orderby=db.address.name)
    return dict(records=records) 

def create(): 
form = SQLFORM(table)
if form.accepts(request.post_vars, session):
   session.flash = 'Address saved.'
   redirect(URL('index'))

== default/index.html ==
{{extend 'layout.html'}} 
<h1>Addresses</h1> 
{{for record in records:}} 
{{=record.name}} : {{=record.address}}<br> 
{{pass}}
[ {{=A('Add Address', _href=URL('create'))}} ]

== default/create.html ==
{{extend 'layout.html'}} 
{{=form}}

Rick

unread,
Dec 13, 2010, 9:41:54 PM12/13/10
to web2py-users
Thanks for the reply. Unfortunately this code idea gives the same
result as mine.

DenesL

unread,
Dec 13, 2010, 10:48:33 PM12/13/10
to web2py-users
Hi Rick,

On Dec 13, 6:55 pm, Rick <sababa.sab...@gmail.com> wrote:
> Hi,
>
> I'm trying to make a data base for storing names and addresses, but
> since I'm a n00b then I can't get it working. I've no idea what's
> wrong. The problem is that the input function doesn't redirect to the
> show function and that the show function doesn't show any data. Here's
> the code:
>
> the controller file:
> def show():
>         id=request.vars.id
>         record=db(db.measure.id==id).select()

record=db(db.addresses.id==id).select()

>         return dict(record=record)
>
> def input():
>         form = SQLFORM(db.addresses)
>         if form.accepts(request.vars, session):
>                 redirect(URL(r=request, f='show'))

redirect(URL(r=request, f='show',vars={'id':form.vars.id}))

>         return dict(form=form)
>
> **********************
>
> and the model file table:
>
> db.define_table('addresses',
>      Field('person'),
>      Field('adress'))

Field('address'))

>
> **********************
>
> input.html
> {{extend 'layout.html'}}
> {{=form}}
>
> **********************
>
> show.html:
> {{extend 'layout.html'}}
> <h1>Addresses</h1>
> {{for record in record:}}
> {{=record.name}} : {{=record.address}}<br>

{{=record.person}} : {{=record.address}}<br>

pbreit

unread,
Dec 13, 2010, 11:10:46 PM12/13/10
to web...@googlegroups.com
Oops, I made same error.

{{=record.name}} : {{=record.address}}<br>

should be

{{=record.person}} : {{=record.address}}<br>

Not to sound like your mother but there were a lot of little errors like that in the original code. You have to be *extremely* careful about the details.

Denes' reply may be better.

DenesL

unread,
Dec 14, 2010, 11:16:43 AM12/14/10
to web2py-users

Correction, you should not set vars using field names already in the
form, otherwise they will be duplicated.

def show():
rid=request.vars.rid
record=db(db.addresses.id==rid).select()

def input():
form = SQLFORM(db.addresses)
if form.accepts(request.vars, session):
redirect(URL(r=request,
f='show',vars={'rid':form.vars.id}))
return dict(form=form)

Rick

unread,
Dec 14, 2010, 12:04:41 PM12/14/10
to web2py-users
Thanks for the help! I got it working with this code:

== controller file ==
def index():
records = db().select(db.addresses.ALL,
orderby=db.addresses.person)
return dict(records=records)

def create():
form = SQLFORM(db.addresses)
if form.accepts(request.post_vars, session):
session.flash = 'Address saved.'
redirect(URL('index'))
return dict(form=form)

== db.py ==
db.define_table('addresses',
Field('person'),
Field('address'))

== index.html ==
{{extend 'layout.html'}}
<h1>Addresses</h1>
{{for record in records:}}
{{=record.person}} : {{=record.address}}<br>
{{pass}}
[ {{=A('Add Address', _href=URL('create'))}} ]

== create.html ==
{{extend 'layout.html'}}
{{=form}}
Reply all
Reply to author
Forward
0 new messages