On Thursday, January 12, 2012 4:37:41 AM UTC-5, Likit wrote: > > I really don't get the use of {{form....}} in views. It's really too > opaque. The form=crud and form=SQLFORM really only do trivial cases > involving a single table. >
I wouldn't say handling a single table is a trivial case, but in fact SQLFORM.factory can handle inputs for multiple tables: http://web2py.com/books/default/chapter/29/7#One-form-for-multiple-tables When the http request comes back with variables for each input field > in the form I want to grab the returned values and put them in the > databases (plural) myself. This is the only way to get the required > control and make the code and view obvious. > > Do I use request.vars to get the returned values? There is something > like form.vars and form.accepts in the manual. But, there is simply > not enough information. I have to process form.accepts before return > dict(form=form). > It's difficult to provide direction without an understanding of what you are trying to do. There's quite a bit of information about form processing in the book -- what specifically do you have questions about? If you want to handle everything yourself manually, you can access the submitted values in request.vars. However, if you don't want to use Crud or SQLFORM, you can still use FORM (http://web2py.com/books/default/chapter/29/7#FORM) to build and validate your form. After validation, the post-validation variables will be in form.vars, and you can do whatever you want with them at that point (e.g., manual inserts into db tables). > Or is it effectively a loop--as long > as the request (the user's reply) comes back to the same url the > controller runs again, but this time with the submitted form. I am > thinking this must be what happens. Yes, that's it. It's called a "self-submitting" form or "postback" and is a common design pattern for web form submission -- see http://web2py.com/books/default/chapter/29/3#Postbacks. Note, in the http://web2py.com/books/default/chapter/29/7#FORM section, it says: The accepts function returns True if the form is accepted and False otherwise. A form is not accepted if it has errors or when it has not been submitted (for example, the first time it is shown). > It is very hard to experiment to > figure this out because of all my runtime errors and the very > challenging diagnostics (layers upon layers of calls through the > framework itself). > Not sure what you mean here. What have you tried to do in terms of diagnostics? What did you expect, and what happened? > > This whole flow thing would be more obvious if I could use yield > instead of return. It seems like what is really needed is something > like this: > > def controller_func(): > prepare info to send to the user in the view > > send the view--e.g., yield dict(form=form) > > receive "request" from the user after submit (or other action) > > do things with what has been received from the user > if form.accepts(?, ?): > x = request.vars.x > db.foo.insert(name=x) etc. > > redirect or next URL > > return (end of function--nothing sent to view) > > The flow above is far more obvious and follows the stateless > transactional model of http. > Actually, the way web2py handles it follows the stateless http model as well. Anyway, there are problems with your proposed approach -- the request would have to remain open indefinitely waiting for the form submission (which may never even happen). This could consume a lot of server resources with many open connections. There are ways of handling this kind of approach via comet (with a suitable server that can handle async communications), but comet is more suitable for real-time applications (e.g., chat) and not really necessary for simple form handling. > Sorry if I am hopelessly stupid, but the manual offers a woeful lack > of complete examples. It includes several examples. What kind of example are you looking for? > There isn't even complete documentation of each > class and all of its methods. http://www.web2py.com/examples/static/epydoc/index.html There are also supposed to be links to the API objects from here ( http://web2py.com/book/default/chapter/04#API), but the links are broken in the new book app (hopefully will be fixed soon). > I did look at the doc strings on the > FORM function (is it a function? a class? ???). Accepts seems to be > the method I want, but it is not clear when it runs. It runs when you call it in your code (see examples in book). > It is not clear > when the validators run. Validators run in the .accept() method (if you call .validate() or .process(), they will ultimately call .accepts()). This is explained here: http://web2py.com/books/default/chapter/29/7#FORM. > It is not clear if I access returned values > (request) with request.vars or form.vars. > form.vars would be best, as they hold the post-validation values (which may include some transformations), but the values are also in request.vars. > I just need more complete code > examples to run and study. There are some complete applications that you can study: http://www.web2py.com/appliances Anthony