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