Thanks for that clarification. This works but it doesn't seem to be
robust.
If I have the data, this works great:
form.vars=request.vars
form.vars.meta_description=request.vars.meta_description.file.read()
db.domain.insert(**dict (form.vars))
However, if there's nothing there (nothing supplied via curl) it fails
silently. I don't get a ticket, the program doesn't crash but the
record is not added. I've tried testing for success using if and try/
except but I can't get it to trigger anything.
form.vars=request.vars
form.vars.meta_description=request.vars.meta_description.file.read()
if db.domain.insert(**dict (form.vars)) :
response.flash="inserted"
else:
response.flash="failed"
Neither works.
If I have to test for the existence of each possible field, I can.
But that's a lot more labor-intensive and error-prone. Every time
you add or modify a field, you need to make sure it has a test. So as
long as I'm the only one using it I'll probably do things on the
sending end - make an empty file and upload @blank
Or maybe I'm missing something?
Thanks very much for the help!
Jim
On Mar 2, 2:42 pm, mdipierro <[email protected]> wrote:
> request.vars.meta_description is a file (a FieldStorage object) not a
> string containing the bytes in the file. It contains
>
> request.vars.meta_description.filename
> request.vars.meta_description.file (the input stream)
>
> So instead of
>
> db.domain.insert(meta_description=request.vars.meta_description)
> ###WRONG!
>
> You want
>
> db.domain.insert
> (meta_description=request.vars.meta_description.file.read())
> ###RIGHT!
>
> Hope this makes sense. This is because you may need to retrieve the
> input filename and because the file size may be large than ram and you
> want to read a bit at the time.
>
> Massimo
>
> On Mar 2, 4:32 pm, Jim <[email protected]> wrote:
>
> > In models/db.py
>
> > SQLField( 'meta_description','text'),
>
> > In controllers/default.py
> > form=SQLFORM(db.domain,_method='POST')
> > form.vars=request.vars
> > db.domain.insert(**dict (form.vars))
>
> > curl -F 'name=flapdoodle.com' -F
> > [email protected]http://127.0.0.1:8002/init/default/add_domain
>
> > I get domain.name=flapdoodle.com just fine. But for the description,
> > it starts out like this:
>
> > FieldStorage('meta_description', 'layout.html', '<!DOCTYPE html PUBLIC
> > "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/
> > xhtml1-strict.dtd">\n<html xmlns="http://www.w3.org/1999/xhtml"
> > ...
> > rest of layout.html
>
> > Is there a way to get the FieldStorage out of there? I only find one
> > reference, on page 180 of the manual.
>
> > PS: I have to say that the (**dict (form.vars)) is pretty freaking
> > cool. It lets me set up reasonable defaults in models, then use
> > those if information is not supplied. So I can have the value for
> > "public" set to default to true, for example.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"web2py Web Framework" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/web2py?hl=en
-~----------~----~----~----~------~----~------~--~---