Below is the difficulty I was having getting request.vars to the
target of the redirect. [add] redirects to [add_2], and when it does,
request.vars doesn't have .x and .y ... instead I find myself stuffing
x and y into the session explicitly and then pulling them out in
[add_2] also explicitly.
Is there something in the framework that handles this? (I feel like
I'm missing something only a newbie would miss). :-) .... maybe
[redirect] or [URL] takes another argument that I don't know about
yet... I hope I've freed you from the notion that I know what I'm
doing :-P
def add():
form=SQLFORM.factory(
Field('x','double',
requires=IS_FLOAT_IN_RANGE(
0,500,
error_message='number between 0 and 500 required')),
Field('y','double',
requires=IS_FLOAT_IN_RANGE(
0,100,
error_message='number between 0 and 100 required')))
if form.accepts(request.vars, session):
session.x = request.vars.x
session.y = request.vars.y
redirect(URL(r=request,f='add_2'))
return dict(form=form)
def add_2():
x=session.x
y=session.y
sum=float(x)+float(y)
return dict(x=x,y=y,sum=sum)
On Oct 5, 11:31 am, mdipierro <[email protected]> wrote:
> Thank you devnull,
>
> some answers below:
>
> On Oct 5, 10:14 am, devnull <[email protected]> wrote:
>
>
>
> > Hello all. Just discovered web2py and I think it's great. I wish we
> > were using it here at work, and so do some of my co-workers.
>
> > I have created a little form as an exercise for myself and I came up
> > with some questions.
>
> > It's likely that some of the things I'm wondering about are handled by
> > the framework and I just don't know it yet...
>
> > ... and the rest of the stuff is probably telltale stylistic noob-ness
> > that I hope you'll point out.
>
> > At the bottom is the controller and view for my form. There's no model
> > yet. The form just adds two values, x and y. Here are the questions.
>
> > 1. The basic structure of the form is defined in the controller part.
> > I did this so I could use the 'requires' argument to the INPUT method
> > to specify a validator. Is this ok? The reason I ask is it feels like
> > I'm putting view stuff in the controller when I say things like TABLE
> > and TR...
>
> You can do
> form=SQLFORM.factory(Field('x',double')),Field('y','double')))
>
> and in view
> {{=form.custom.begin}}
> {{=form.custom.widget.x}}
> {{=form.custom.widget.y}}
> {{=form.custom.submit}}
> {{=form.custom.end}}
> then insert the HTML you need in the view.
>
> > 2. I wanted a more specific error message for each field which uses an
> > is-float-in-range validator ... But is there a way to refer to the
> > actual max and min without repeating it in the string? Something like
> > $max and $min?
>
> No, there is not. We could add it. If we do it would be %{minimum}s %
> (maximum)s. Pros/Cons?
>
> > 3. At the end of the controller, am I making a mistake by passing x
> > and y explicitly? Are they also hiding inside form and can I extract x
> > and y from the form in the view html?
>
> You are not but form.vars.x is available inside the view since you are
> passing the form.
>
> > 4. Would it be a better practice to redirect the user to another page
> > for the result instead of doing everything on the same page? I tried
> > this but had some difficulty getting x and y in the second page for
> > processing (adding).
>
> I like to redirect on accept. In this case there is no different but
> you may have multiple objects in one page that depend on one another.
> Without an explicit redirection it ma be difficult to keep track of
> their state.
>
> > 5. Do you have any other advice?
>
> No. It seems to me you know aht you are doing.
>
>
>
> > Looking forward to learning from you all. Thanks in advance!
>
> > controller:
>
> > def form_example():
> > form=FORM(TABLE(
> > TR('x',
> > INPUT(_type='text',_name='x',
> > requires=IS_FLOAT_IN_RANGE(
> > 0,500,
> > error_message='number between 0 and 500
> > required'))),
> > TR('y',
> > INPUT(_type='text',_name='y',
> > requires=IS_FLOAT_IN_RANGE(
> > 0,100,
> > error_message='number between 0 and 100
> > required'))),
> > TR('',INPUT(_type='submit',
> > _value='add'))))
> > sum = x = y = ''
> > visibility = 'hidden'
> > if form.accepts(request.vars, session):
> > sum = float(request.vars.x) + float(request.vars.y)
> > visibility = 'visible'
> > return dict(form = form,
> > sum = sum,
> > x = request.vars.x,
> > y = request.vars.y,
> > visibility = visibility)
>
> > view:
>
> > <h1>form example</h1>
> > {{=form}}
> > <table style="visibility:{{=visibility}}">
> > <tr>
> > <td>Most recent x entered</td>
> > <td>{{=x}}</td>
> > </tr>
> > <tr>
> > <td>Most recent y entered</td>
> > <td>{{=y}}</td>
> > </tr>
> > <tr>
> > <td>sum</td>
> > <td>{{=sum}}</td>
> > </tr>
> > </table>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"web2py-users" 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
-~----------~----~----~----~------~----~------~--~---