redirection to next page on submit

985 views
Skip to first unread message

Nazgi

unread,
May 3, 2009, 9:50:04 AM5/3/09
to web2py Web Framework
Hi,

I'm new to python and web2py. I have a simple doubt. I've
written a form and after the submit button is pressed in the form, I
want to print the variables of the form into next page and ask for
confirmation. I have written the form in a controller and also the
function for the next page where the variables of the form are
printed. But i dont know how to direct to that page after pressing of
the submit button. Can someone help me regarding this.

regards,
Nazgi

mdipierro

unread,
May 3, 2009, 10:01:40 AM5/3/09
to web2py Web Framework
def page1():
form=SQLFORM(db.a_table)
if FORM.accepts(form,request.vars):
session.vars=form.vars
redirect(URL(r=request,f='confirm'))
return dict(form=form)

def confirm():
form=FORM('sure?',INPUT(_name='yes',_type='checkbox'),INPUT
(_type='submit'))
if request.vars.yes and session.vars:
db.a_table.insert(**session.vars)
... do something
return dict(form=form)

newbie

unread,
May 3, 2009, 10:03:03 AM5/3/09
to web2py Web Framework
thanks for the immediate reply :)

newbie

unread,
May 3, 2009, 1:53:09 PM5/3/09
to web2py Web Framework
hi mdipierro,

Actually I'm trying to access some of the request variables of
the form. But If i redirect using

redirect(URL(r=request,f='confirm'))

I'm not able to access the request variables of the form.

Fran

unread,
May 3, 2009, 2:46:41 PM5/3/09
to web2py Web Framework
On May 3, 6:53 pm, newbie <mara.ku...@gmail.com> wrote:
>        Actually I'm trying to access some of the request variables of
> the form. But If i redirect using
>         redirect(URL(r=request,f='confirm'))
>      I'm not able to access the request variables of the form.

Try copying them to session before the redirect?

F

Yarko Tymciurak

unread,
May 3, 2009, 4:19:37 PM5/3/09
to web...@googlegroups.com
The thing to be aware of:

www is connectionless;  each request is it's own "program" running (sort of - it's its own thread).

That is, when you ask for a page, a new instance of web2py is created (cache), the application and controller that is being requested is determined, the models are read into the thread so that the data structures for your datastore are defined (actually, all the files in your app/models directory are run, so you may find some opportunistic placement of utilities into the models directory by some - this is ok)...  and once that environment is setup for the thread, the function from the controller is called.

You have a few ways to communicate information between these threads:

- store in persistent data, and let each thread read it out;
- pass in parameters to your controller/function call
     See the URL function: you'll see all paths to web2py apps have the form of:
     - application/controller/function/arg/arg2/.../vars
    the last piece looking like a dict:   ....arg/arg2?key=value,key2=another_value
- store in session variables (short term, and works for the same client session)

newbie

unread,
May 4, 2009, 2:37:44 AM5/4/09
to web2py Web Framework
Thank you. wud try copying into session variables. :)

On May 4, 1:19 am, Yarko Tymciurak <yark...@gmail.com> wrote:
> The thing to be aware of:
>
> www is connectionless;  each request is it's own "program" running (sort of
> - it's its own thread).
>
> That is, when you ask for a page, a new instance of web2py is created
> (cache), the application and controller that is being requested is
> determined, the models are read into the thread so that the data structures
> for your datastore are defined (actually, all the files in your app/models
> directory are run, so you may find some opportunistic placement of utilities
> into the models directory by some - this is ok)...  and once that
> environment is setup for the thread, the function from the controller is
> called.
>
> You have a few ways to communicate information between these threads:
>
> - store in persistent data, and let each thread read it out;
> - pass in parameters to your controller/function call
>      See the URL function: you'll see all paths to web2py apps have the form
> of:
>      - application/controller/function/arg/arg2/.../vars
>     the last piece looking like a dict:
> ....arg/arg2?key=value,key2=another_value
> - store in session variables (short term, and works for the same client
> session)
>

Kuba Kucharski

unread,
May 7, 2009, 5:25:06 PM5/7/09
to web...@googlegroups.com
> www is connectionless;  each request is it's own "program" running (sort of
> - it's its own thread).
[snip]

>
> You have a few ways to communicate information between these threads:
>
> - store in persistent data, and let each thread read it out;

[snip]

Again, probably lame one..

I know it is not thread safe, but I have an app, an xml listener,
there is only one thread talking to it, for sure. so I want to use
persistent storage. database can do, but what are the other options? I
would like a real global variable. Is it possible?

I cannot use sessions nor passing params. This is not my own app
talking to my listener. The problem is I have doubled records because
of double POST method to my controller(possible bug in freeswitch
curl, boken retries support). I would like to filter out this
redundancy. So I thought I will compare for similarity.. But to
compare I need to know the previous value.. which means from a
previous POST.. Is there a recipe for me?

--
Kuba

mdipierro

unread,
May 7, 2009, 5:36:18 PM5/7/09
to web2py Web Framework
It really depends on details.

If you really need a global variable you can do

class ThreadSafe(dict):
forever=10**10
def __init__(self):
import thread
self['lock']=thread.allocate_lock()
def __setattr__(self,key,value):
self['lock'].acquire()
self[key]=value
self['lock'].release()
def __getattr__(self,key):
self['lock'].acquire()
value=self[key]
self['lock'].release()
return value

and where you need it use it as:

x=cache.ram('my_thread_safe_object',lambda:ThreadSafe
(),ThreadSafe.forever)
x.value=4
print x.value

If instead you need a backgroud process create one with

python web2py.py -S app -M -R your_backround_process.py

and have the process create (for example) a XMLRPC service to talk to
your apps.

Massimo
Reply all
Reply to author
Forward
0 new messages