onvalidation form.vars empty

554 views
Skip to first unread message

António Ramos

unread,
Jan 15, 2013, 10:17:08 AM1/15/13
to web...@googlegroups.com
hello reading the book online i tried the code


def my_form_processing(form):
    c = form.vars.a

    form.vars.b
    if c < 0:
        form.errors.b = 'a'
    else:
        form.vars.c = c



but form.vars was empty

to access my form vars i changed to form.request_vars 

Is the book wrong?

Thank you
António

Anthony

unread,
Jan 15, 2013, 2:28:08 PM1/15/13
to web...@googlegroups.com
form.vars should work. Can you show more code? Note, form.request_vars is simply a copy of the original request.post_vars, so not equivalent to form.vars.

Anthony

António Ramos

unread,
Jan 15, 2013, 5:07:30 PM1/15/13
to web...@googlegroups.com
I´m testing this book example
I get error on field named 'a'

Traceback (most recent call last):
File "C:\web2pyGit\web2py\gluon\restricted.py", line 212, in restricted
exec ccode in environment
File "C:/web2pyGit/web2py/applications/teste1/models/db.py", line 91, in <module>
Field('c', 'integer', readable=False, writable=False))
File "C:\web2pyGit\web2py\gluon\dal.py", line 7189, in define_table
table = self.lazy_define_table(tablename,*fields,**args)
File "C:\web2pyGit\web2py\gluon\dal.py", line 7206, in lazy_define_table
table = table_class(self, tablename, *fields, **args)
File "C:\web2pyGit\web2py\gluon\dal.py", line 7612, in __init__
db.check_reserved_keyword(field_name)
File "C:\web2pyGit\web2py\gluon\dal.py", line 6933, in check_reserved_keyword
'invalid table/column name "%s" is a "%s" reserved SQL keyword' % (name, backend.upper()))
SyntaxError: invalid table/column name "a" is a "ALL" reserved SQL keyword

So i renamed all to a1,b1,c1 and it works !

Now my initial complain is still not working. I will test tomorrow and post some code.
Meanwhile please check why a is not a valid field name and update the book if necessary.
thank you

2013/1/15 Anthony <abas...@gmail.com>
--
 
 
 

António Ramos

unread,
Jan 16, 2013, 7:54:07 AM1/16/13
to web...@googlegroups.com
can onvalidation set upload  file field?

i was complaining that form.vars was empty.
It is if the controller does not return the form to the view
So i return the form and away with my quest.

Now, in onvalidation i set  the field with the upload file.
I have to becaue i have a different upload button that goes online to get a file.
Its working but the image file i set goes as a txt file to the database, why


here my code:
(I use PIL module)

if trabalhador_form.process(onvalidation=attach_image_to_record).accepted:

def attach_image_to_record(form):

    response=requests.get(form.vars.fp_url)
    img=Image.open(StringIO(response.content))
    img.save('foto1231.jpg','JPEG')
    temp=open('foto1231.jpg','rb')
    photo=db.trabalhador.foto.store(temp,'foto.jpg')
    print photo   # echoes something like     "trabalhador.foto.989708787087078.9823492034.jpg
    form.vars.photo=photo  # here is where someting goes wrong....

This is very important for me ,
can anyone explain why a jpg is inserted as a txt? how to force jpg ?

Thank you
António


2013/1/15 António Ramos <ramst...@gmail.com>

António Ramos

unread,
Jan 16, 2013, 8:49:30 AM1/16/13
to web...@googlegroups.com
I just noticed one image created in upload folder and one txt file.
Inside the txt file i see the name of jpg file created above.


why is that?

help please :)

António

2013/1/16 António Ramos <ramst...@gmail.com>

Massimo Di Pierro

unread,
Jan 16, 2013, 10:44:45 PM1/16/13
to web...@googlegroups.com
You are storing the image twice:

if trabalhador_form.process(onvalidation=attach_image_to_record).accepted: # here after validation 
def attach_image_to_record(form):
    ...
    photo=db.trabalhador.foto.store(temp,'foto.jpg') # here on validation
    ... 
    form.vars.photo=photo  # here is where someting goes wrong....

The problem is that you altering the form on validation.


This this:


def attach_image_to_record(form):
    import cgi
    response=requests.get(form.vars.fp_url)
    img=Image.open(StringIO(response.content))
    img.save('foto1231.jpg','JPEG')
    photo = cgi.FieldStorage()
    photo.file = open('foto1231.jpg','rb')
    photo.filename = 'foto.jpg'
    form.vars.photo=photo # not this on, you let process do upload




This is very important for me ,

António Ramos

unread,
Jan 21, 2013, 12:35:00 PM1/21/13
to web...@googlegroups.com
can onvalidation be used with sqlform.factory ?

i´m getting error

Errors in Form. Please check it out!!!


Thank you


António

2013/1/17 Massimo Di Pierro <massimo....@gmail.com>
--
 
 
 

Massimo Di Pierro

unread,
Jan 21, 2013, 12:57:54 PM1/21/13
to web...@googlegroups.com
yes.

António Ramos

unread,
Jan 21, 2013, 1:03:11 PM1/21/13
to web...@googlegroups.com
Sorry for insisting,

I ´m using onvalidation in sqlform but if i use it in sqlform.factory i get a flash message
Errors in Form. Please check it out!!!

Secondly
as i am using sqlform.factory to add records how to use it also to update or delete?

Thank you
António

2013/1/21 Massimo Di Pierro <massimo....@gmail.com>
--
 
 
 

Massimo Di Pierro

unread,
Jan 21, 2013, 2:18:30 PM1/21/13
to web...@googlegroups.com
Here is my test. 

$ python web2py.py -S welcome
(InteractiveConsole)
>>> import sys
>>> form = SQLFORM.factory(Field('name'))
>>> form.process(onvalidation=lambda form:sys.stdout.write('check!\n'),formname=None,session=None)
check!

It works. Something else is wrong in your code. If you could post a complete example we can try help you debug it.

Massimo

António Ramos

unread,
Jan 21, 2013, 4:25:07 PM1/21/13
to web...@googlegroups.com
Thank you Massimo, you are always available to help. Where is Bill Gates when we need it right?

I restarted web2py and it works....

Strange....

Second question.
as i am using sqlform.factory to add records how to use it also to update or delete?


Thank YOU again!

António


--
 
 
 

Massimo Di Pierro

unread,
Jan 21, 2013, 5:29:44 PM1/21/13
to web...@googlegroups.com
If you want want to interact with a database table why use SQLFORM.factory and not simply SQLFORM?

António Ramos

unread,
Jan 21, 2013, 5:36:42 PM1/21/13
to web...@googlegroups.com
I need a multi file uploader. Web2py does not have it yet.
I saw an example using another table for the files.
I dont know how to sqlform 2 tables.
The book showed me how to do it with sqlform.factory 



Thank you
António

--
 
 
 

Reply all
Reply to author
Forward
0 new messages