Inline images in SQLFORM, general SQLFORM API...

17 views
Skip to first unread message

Peter

unread,
Dec 28, 2008, 4:42:56 PM12/28/08
to web2py Web Framework
I need to display a form with images inline, ie with <img> tags. The
default behavior for SQLFORM is to only show a <file> link on the form
(assuming I provide a download URL in the constructor, which I do).

What's the best way to do this -- if at all? Again, I have difficulty
"breaking into" the SQLFORM object... Am I doomed to write my own
upload logic?

BTW, I would really appreciate a more formal API for SQLFORM that is
more geared towards using it piecemeal rather than the single
{{=form}}, which is suitable less than half of my current
application's forms. Ideally, any magic that SQLFORM provides should
be exposed in a lower-level API as well...

Note: I'm not a hardcore Pythonhead, more the type of "enterprise"
developer that I think Massimo seems to have had in mind when he
called web2py a "enterprise" framework. ;-) I thought I would be able
to get going quickly with SQLFORM and T2 CRUD, but so far, it's been a
bit of an uphill battle, I'm afraid... All the more frustrating
because I think it's very very close to what I need.

Cheers,
-Peter

Fran

unread,
Dec 28, 2008, 6:14:20 PM12/28/08
to web2py Web Framework
On Dec 28, 9:42 pm, Peter <peter.kleyn...@gmail.com> wrote:
> BTW, I would really appreciate a more formal API for SQLFORM that is
> more geared towards using it piecemeal rather than the single
> {{=form}}, which is suitable less than half of my current
> application's forms.

I think the new form.element() functionality just added to trunk
should be helpful to you:
http://groups.google.com/group/web2py/browse_thread/thread/2d8cc57352d158bd/

> I thought I would be able
> to get going quickly with SQLFORM and T2 CRUD, but so far, it's been a
> bit of an uphill battle, I'm afraid... All the more frustrating
> because I think it's very very close to what I need.

I've found that with a little work, T2 does indeed do all that I want,
other than for when I need to build a form out of multiple tables :)

I use a mixture of these:
db.table.exposes
db.table.displays
db.table.represent
db.table.contact.label
db.table.field.comment

F

Peter

unread,
Dec 28, 2008, 7:02:53 PM12/28/08
to web2py Web Framework
Fran, thanks.

Yeah, I had seen the .element(...) mod. That should allow me to locate
and rip the download URL from the HTML tree and put it in an IMG tag.
Not the prettiest way, and I would like to have something that works
in production 1.54. So I will maintain my question on how to retrieve
the download URL from a SQLFORM.

Re T2 -- my work process over the past weeks (on and off) with web2py
has been to try T2, to get stuck, then to try SQLFORM, to get stuck
again, and to finally hack my way to a solution using a mix of
SQLFORM, regular FORM and/or form_factory -- with some superfast
support from this forum, where needed. More than anything else, it's
the lack of a single ONE way to do it that is frustrating and time
consuming: having to try X different ways to get (IMO) basic
functionality to work.

Given my own experiences, I am surprised you found that T2 does most
of what you need. Maybe I should give it another shot. Care to share
some T2 secrets? ;-)

Cheers,
-Peter

On Dec 29, 12:14 am, Fran <francisb...@googlemail.com> wrote:
> On Dec 28, 9:42 pm, Peter <peter.kleyn...@gmail.com> wrote:
>
> > BTW, I would really appreciate a more formal API for SQLFORM that is
> > more geared towards using it piecemeal rather than the single
> > {{=form}}, which is suitable less than half of my current
> > application's forms.
>
> I think the new form.element() functionality just added to trunk
> should be helpful to you:http://groups.google.com/group/web2py/browse_thread/thread/2d8cc57352...

mr.freeze

unread,
Dec 28, 2008, 7:05:09 PM12/28/08
to web2py Web Framework
You might try using SQLFORM's col3 parameter. It comes in handy for
me.
Message has been deleted

Peter

unread,
Dec 29, 2008, 6:21:55 AM12/29/08
to web2py Web Framework
Thank you -- based on all of your input I'm having another stab at
using T2.
Two more questions:

- How do you pass variables to T2-enabled pages without breaking T2
behaviors? For instance, I have a page where person X buys product P;
prod_id is passed as a var or an arg in the query string. The T2 form
shows person X's details, including an inline image (very nice ;-)),
but the #zoom anchor doesn't work because the prod_id in the URL gets
in the way. What's the best practice here?? I can sneak prod_id into
the session vars, but that doesn't seem very RESTful -- apart from it
being a pain not being able to embed the prod_id in a link...

- Call me stupid, I can't find the way to rename the submit button on
T2 update/create forms? I know you can do this with submit_button= in
SQLFORM, but this doesn't seem to get passed into T2 forms. The only
way is to manipulate the components[..] stuff -- eg, through the new
elements interface?

Cheers again...
-Peter


On Dec 29, 7:10 am, mdipierro <mdipie...@cs.depaul.edu> wrote:
> define a widget, something like:
>
> def myhelper(field,value):
>     field_id='%s_%s' % (field._tablename,field.name)
>     inp=INPUT(_type='file',_id=field_id,_class=field.type,
>                             _name=field.name, requires=field.requires)
>     if not value: return inp
>     url='app/controller/download/'+value  ###### edit this line
>     return DIV(inp,IMG(_src=url,_width=200),
>                     INPUT(_type='checkbox',_name=field.name
> +'__delete'),'delete]')
>
> tell web2py that the field should be represented by the widget.
>
> works with sqlform, t2 and t3.
>
> Massimo
>
> db.yourtable.yourfield.represent=myhelper
>
> On Dec 28, 3:42 pm, Peter <peter.kleyn...@gmail.com> wrote:
>

mdipierro

unread,
Dec 29, 2008, 8:42:52 AM12/29/08
to web2py Web Framework
CORRECTION:

define a widget, something like:

def myhelper(field,value):
field_id='%s_%s' % (field._tablename,field.name)
inp=INPUT(_type='file',_id=field_id,_class=field.type,
_name=field.name, requires=field.requires)
if not value: return inp
url='app/controller/download/'+value ###### edit this line
return DIV(inp,IMG(_src=url,_width=200),
INPUT(_type='checkbox',_name=field.name
+'__delete'),'delete]')

tell web2py that the field should be represented by the widget.

db.yourtable.yourfield.widget=myhelper


works with sqlform, t2 and t3.

Massimo


mdipierro

unread,
Dec 29, 2008, 9:21:55 AM12/29/08
to web2py Web Framework
1) I am sorry. I do not understand without a code example.

2) not possible. I agree there should be a way to do it.
Using the latest trunk you can do

form.element(_type='submit').update(_value='click me!')

Massimo

On Dec 29, 5:21 am, Peter <peter.kleyn...@gmail.com> wrote:

Peter

unread,
Dec 29, 2008, 11:47:00 AM12/29/08
to web2py Web Framework
Hi Massimo, thanks for helping out again...

Re (1), interaction between T2 and args passed in the query string,
here's the basic code:

in default.py:
def buy_one():
prod=db(db.product.id==request.args[0]).select()[0]
form = t2.display(db.t2_person,query=
(db.t2_person.id==t2.person_id))
return dict(form=form,prod=prod)

in buy_one.html:
{{extend 'layout.html'}}
<!-- ...some use of { {=prod.fields} }, presentation of product to
buy... -->
{{=form}} <!-- show personal details, present edit -->
<p><a href="{{=URL(r=request,f='edit_details',vars=dict
(next='buy_one'))}}">Click here</a> to edit your details.</p><br/>

The initial URL is eg ".../default/buy_one/3" (ie, prod_id=3)
When I click on the image presented (very nicely, by the way) by
t2.display, the resulting URL is
>>> "...buy_one/3#zoom-t2_person-foto-41" which clearly can't work.
Using a request.var (?prod_id=3) leads to a similar result.

None of this is a dealbreaker, but I was starting to wonder if it was
generally unwise (ie, stupid) to use request.args and .vars in
combination with T2 forms.
The default "args[-1]=t2.id" behavior in T2 also points in that
direction. If it's just a fluke, I can try to work around it. Elements
(...) again... ;-)

Re (2), using the elements(...) fix-- it IS attractive for lots of
little issues I have. When do you expect this functionality to appear
in a production release? If that is within a week or two, I can use
trunk for my current project...

Cheers & thanks for a great tool,
-Peter

Peter

unread,
Dec 29, 2008, 12:38:46 PM12/29/08
to web2py Web Framework
> form.element(_type='submit').update(_value='click me!')

Massimo, I just tried the above with t2.update using trunk (1.55rc1):

form = t2.update(db.t2_person,query=
(db.t2_person.id==t2.person_id),next=nextpage,deletable=False)
form.element(_type="submit").update(_value="Save")

The substitution of the button label works fine, but further down the
form something goes completely haywire. I get the form fields
splattered across my screen four or five times.

Looking at the generated html: after the closing </table> tag in the
form, after the <input name="id"...>, it should have <input
name="modified_on"...> but instead it starts afresh with <tr
id="t2_person_name_row">, ie, the top table row. I can send you both
html outputs if you like.

Cheers,
-Peter

On Dec 29, 3:21 pm, mdipierro <mdipie...@cs.depaul.edu> wrote:

mdipierro

unread,
Dec 29, 2008, 1:45:57 PM12/29/08
to web2py Web Framework
yes, please send me the code than output (form only)

Massimo

mdipierro

unread,
Dec 29, 2008, 2:17:26 PM12/29/08
to web2py Web Framework
I am till confused about (1).

The url "...buy_one/3#zoom-t2_person-foto-41" is not generated by T2.
Do you have

db.table.field.represent=... somewhere?

(2) The trunk will be promoted to production in about one week.

Massimo
Reply all
Reply to author
Forward
0 new messages