#2 total newbie using CherryPy & SQLObject

Maureen Black

The following code sample seems to only update via
cache, and I don't see the actual record updated nor
inserted in the database when I call 'store'. I turned
on debugging and I can see the SQL statements. I can
attach to MySQL and query it directly via localhost.
select works fine, too. I can see a row I manually
enter via the web form.

Any assistance is appreciated!


from cherrypy import cpg
from Cheetah.Template import Template
from sqlobject.mysql.mysqlconnection import MySQLConnection

conn = MySQLConnection(user='root', db='test',
passwd="", debug=1)

# this is our (only) data class.
class Contact(SQLObject):
_connection = conn
lastName = StringCol(length = 50, notNone = True)
firstName = StringCol(length = 50, notNone = True)
phone = StringCol(length = 30, notNone = True,
default = '')
email = StringCol(length = 30, notNone = True,
default = '')
url = StringCol(length = 100, notNone = True,
default = '')

class ContactManager:
def index(self):
# Let's display a list of all stored contacts.
contacts = Contact.select()

template = Template('''
<h2>All Contacts</h2>

#for $contact in $contacts
[<a href="./edit?id=$contact.id">Edit</a>]
#end for

<p>[<a href="./edit">Add new contact</a>]</p>
''', [locals(), globals()])

return template.respond()

index.exposed = True

def edit(self, id = 0):
# we really want id as an integer. Since
GET/POST parameters
# are always passed as strings, let's convert it.
id = int(id)

if id > 0:
# if an id is specified, we're editing an
existing contact.
contact = Contact.get(id)
title = "Edit Contact"
# if no id is specified, we're entering a
new contact.
contact = None
title = "New Contact"

# In the following template code, please note
that we use
# Cheetah's $getVar() construct for the form
values. We have
# to do this because contact may be set to None
(see above).
template = Template('''

<form action="./store" method="POST">
<input type="hidden" name="id"
value="$id" />
Last Name: <input name="lastName"
value="$getVar('contact.lastName', '')" /><br/>
First Name: <input name="firstName"
value="$getVar('contact.firstName', '')" /><br/>
Phone: <input name="phone"
value="$getVar('contact.phone', '')" /><br/>
Email: <input name="email"
value="$getVar('contact.email', '')" /><br/>
URL: <input name="url"
value="$getVar('contact.url', '')" /><br/>
<input type="submit" value="Store" />
''', [locals(), globals()])

return template.respond()

edit.exposed = True

def delete(self, id):
# Delete the specified contact
contact = Contact.get(int(id))
return 'Deleted. <a href="./">Return to Index</a>'

delete.exposed = True

def store(self, lastName, firstName, phone, email,
url, id = None):
if id and int(id) > 0:
# If an id was specified, update an
existing contact.
contact = Contact.get(int(id))

# We could set one field after another, but
that would
# cause multiple UPDATE clauses. So we'll
just do it all
# in a single pass through the set() method.
lastName = lastName",
firstName = firstName,
phone = phone,
email = email,
url = url)
# Otherwise, add a new contact.
contact = Contact(
lastName = lastName,
firstName = firstName,
phone = phone,
email = email,
url = url)

return 'Stored. <a href="./">Return to Index</a>'

store.exposed = True

def reset(self):
# Drop existing table

# Create new table

# Create some sample data
firstName = 'testfirstName',
lastName = testLastName',
email = 'test@test.com',
phone = '555-1212',
url = 'http://www.nope.com')

return "reset completed!"

reset.exposed = True

print "If you're running this application for the first
time, please go to http://localhost:8080/reset once in
order to create the database!"

cpg.root = ContactManager()
cpg.server.start(configFile = 'tutorial.conf')


  • Oleg Broytman
    Oleg Broytman

    Logged In: YES

    The code is too big and badly formatted. Please prepare a
    shorter example and attach it as a text file.

  • Oleg Broytman
    Oleg Broytman

    • status: open --> closed