Hi Jeff -

Have a look at the examples in Chapter 3 - 
http://www.web2py.com/book/default/section/3,
particularly leading into http://www.web2py.com/book/default/section/3/5

This should help you get your example working.

It is also helpful to think of the flow through web2py of the
request / reqponse:

brower sends a request
server finds the webapplication  web2py  (in the default case, the
server is builtin to web2py, but the flow is still the same
reqardless)
within web2py:  gluon/main  finds the appropriate web2py application &
controller to call
main() sets up the environment for the application controller with all
the models files run (in context) before calling the controller
The controller sets up a context to return a response to the
webbrowser (client), and "returns" a dict to main().
main()  finds the appropriate view (same name as the controller) and
processes the view & templates, with the dict passed by the
controller, renders and returns to the webbrowser.

For forms this is the thing to remember:   the SECOND call to the
controller comes from the client (normally with the same URI, i.e. a
second call to the same controller function).

This means that the controller with forms (i.e. which wants to save
data from a client interaction)
will:
1/ setup a form
2/ check if the form has been filled out (or if it's a first call);
3/ do error checking / validation to ensure values are valid, and
possibly return to the browser without storing;
4/ finally, store the values (much is done for you in form.accepts()),
and possibly redirect or display success to the browser.

Keeping this flow in mind should make the structure shown in
http://www.web2py.com/book/default/section/7/1  make sense,  e.g.:

def  my_controller:
     form = FORM(....)
     ...
     if form.accepts(....):
        # valid entries made
    else if form.errors:
        # return to the client with feedback;
    return dict(form=form)    # pass the form to the view as the view
variable 'form'

.... in the view, you don't need / want all the manual form setup;
you want to render the form object, thus:

{{=form}}

-----

Hopes this helps you get started.   Depending on the level of class,
there are other tutorials / dojos which
are online, were presented at conference workshops.  See, for example,
the Videos link on the book page.

I understand Massimo is away, not readily accessible to the net, but
I'm sure he will point you to more.
In the meantime, I hope this helps.
Let us know if you need more.

Regards,
- Yarko

On Jun 14, 9:37 am, Jeff Elkner <[email protected]> wrote:
> It is the end of the school year and my students and I are trying to
> create simple projects using web2py.
>
> Can you point me to a "simple" example of web2py creating and
> accessing a database?  By "simple" I mean from the teaching
> perspective.  I wanted to create a little application that stored
> quotes in a database.  Here is what I have so far:
>
> The Controller (default.py):
>
> def index():
>    return dict()
>
> def addquote():
>    form = SQLFORM(db.quotes)
>    return dict()
>
> The Views (index.html):
>
> <?xml version="1.0" encoding="UTF-8"?>
> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
> "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd";>
> <html xmlns="http://www.w3.org/1999/xhtml"; xml:lang="en">
> <head>
> <title>Quotable Quotes</title>
> <link rel="stylesheet" type="text/css" href="../static/style.css" />
> </head>
> <body>
> <h1>Quotable Quotes</h1>
>
> <h2>Choose:</h2>
> <ul>
>  <li><a href="addquote">Add a quote to the database.</a></li>
> </ul>
>
> </body>
> </html>
>
> and addquote.html:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
> "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd";>
> <html xmlns="http://www.w3.org/1999/xhtml"; xml:lang="en">
> <head>
> <title>Quotable Quotes</title>
> <link rel="stylesheet" type="text/css" href="../static/style.css" />
> </head>
> <body>
> <h1>Quotable Quotes</h1>
>
> <form>
> <fieldset>
>  <legend>Quote:</legend>
>  <textarea name="quote" rows="5" cols="100"></textarea>
> </fieldset>
> <fieldset>
>  <legend>Author:</legend>
>  <input type="text" name="author" />
> </fieldset>
> <button type="submit">Submit</button>
> <input type="hidden" name="_formname" value="addquote" />
> </form>
>
> </body>
> </html>
>
> and finally the db.py file:
>
> db = DAL('sqlite://storage.sqlite')
>
> db.define_table('quotes', Field('quote'), Field('author'))
>
> ++++++++++++++++++++++++++++++++++++++++++++++
>
> The database gets created, and when I visit the addquote.html view, I
> can fill in the forms and click the submit button without error, but
> no rows are added to the table.
>
> How can I get that to work?  The database chapter in the book is not
> very helpful to a database beginner such as myself, since all the
> examples are from a shell, and it doesn't show how to connect the
> pieces together inside an application.
>
> Thanks!
>
> jeff elkner

Reply via email to