Help with picture default

10 views
Skip to first unread message

CesarBustios

unread,
Nov 6, 2010, 2:32:01 AM11/6/10
to web2py-users
Hi, please in my database i have an 'upload' type Field and i want to
make some picture ('pic.jpg') the default picture when the form load,
how can i do that???

I really hope you can help me out, Thank you!

mdipierro

unread,
Nov 6, 2010, 3:41:51 AM11/6/10
to web2py-users
This should do it.

db.define_table('mytable', Field('picture','upload'))

db.mytable.picture.default=lambda:db.mytable.picture.store(open('path/
pic.jpg','rb'))

CesarBustios

unread,
Nov 6, 2010, 9:39:33 AM11/6/10
to web2py-users
Thanks a lot Mr. Massimo you save my life :) .... I love web2py by the
way!
> > I really hope you can help me out, Thank you!- Ocultar texto de la cita -
>
> - Mostrar texto de la cita -

CesarBustios

unread,
Nov 6, 2010, 10:06:34 AM11/6/10
to web2py-users
Oh no it's not working! =(
> > - Mostrar texto de la cita -- Ocultar texto de la cita -

mdipierro

unread,
Nov 6, 2010, 11:22:29 AM11/6/10
to web2py-users
My mistake... this is a little more complex than I thought because it
depends on the workflow. There are two options:

1) if the user does not set an image you get a default one

assuming :
db.define_table('mytable', Field('picture','upload'))
and assuming:
form=crud.create(db.mytable)

you can do:

def addimage(form):
if not form.vars.picture:
form.vars.picture=db.mytable.picture.store(open('path/
pic.jpg','rb'))

form=crud.create(db.mytable,
onvalidation=lambda form:addimage(form))

This should work.

2) you want to show the user the default hence you must create a dummy
record and always use crud update instead of crud create. This is more
complex.

Massimo

Branko Vukelic

unread,
Nov 6, 2010, 4:25:43 PM11/6/10
to web...@googlegroups.com
On Sat, Nov 6, 2010 at 7:32 AM, CesarBustios <cesarb...@gmail.com> wrote:
> Hi, please in my database i have an 'upload' type Field and i want to
> make some picture ('pic.jpg') the default picture when the form load,
> how can i do that???

For something like an avatar, where user gets assigned a default one
if none is uploaded, you are better off handling that at display time
rather than create time.

When you read the database record you can do something like:

mypic = record.picture or something_else

Would that work for you?


--
Branko Vukelić

bg.b...@gmail.com
stu...@brankovukelic.com

Check out my blog: http://www.brankovukelic.com/
Check out my portfolio: http://www.flickr.com/photos/foxbunny/
Registered Linux user #438078 (http://counter.li.org/)
I hang out on identi.ca: http://identi.ca/foxbunny

Gimp Brushmakers Guild
http://bit.ly/gbg-group

CesarBustios

unread,
Nov 7, 2010, 5:28:33 PM11/7/10
to web2py-users
Thanks a lot! I'll try both :)

On Nov 6, 3:25 pm, Branko Vukelic <bg.bra...@gmail.com> wrote:
> On Sat, Nov 6, 2010 at 7:32 AM, CesarBustios <cesarbust...@gmail.com> wrote:
> > Hi, please in my database i have an 'upload' type Field and i want to
> > make some picture ('pic.jpg') the default picture when the form load,
> > how can i do that???
>
> For something like an avatar, where user gets assigned a default one
> if none is uploaded, you are better off handling that at display time
> rather than create time.
>
> When you read the database record you can do something like:
>
>     mypic = record.picture or something_else
>
> Would that work for you?
>
> --
> Branko Vukelić
>
> bg.bra...@gmail.com
Reply all
Reply to author
Forward
0 new messages