#model
db=DAL('sqlite://storage.db')
from gluon.tools import *
auth=Auth(globals(),db)
crud=Crud(globals(),db)

db.define_table('quote',Field('message'))

#controller
@auth.requires_login()
def index():
      form=crud.create(db.quote)
      quotes=db(db.quote.id>0).select()
      return dict(form=form, quotes=quotes)

def user(): return dict(form=auth())

def download(): return response.download(request,response,db)

# view default/index.html
{{extend layout.html'}}
<h1>Post</h1>
{{=form}}
<h2>Previous quotes</h2>
{{for quote in quotes:}}{{=quote.message}}<br/>{{pass}}

You catch me at the wrost possible time. I am at a conference and have limited connection. Sorry.

On Jun 14, 2010, at 9:37 AM, Jeff Elkner 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