Passing Arguments

by Mike Takahashi last modified Feb 04, 2009 02:34 AM

Passing arguments with MySQL and Python to create insert, update and delete statements.

We’ve been able to successfully display data from a MySQL database in Plone, but what about inserting, updating, or deleting data via Plone? In this example, we’ll go over how to insert data into a MySQL database. You’ll be able to take these same principles and apply them to update and delete data.

There are many ways to do this, but for this tutorial we will be creating a form within a page template and submitting the values to a Python script, which will then insert the data into a MySQL database.


Creating the Form

Create another new page template with the id addnames.

Insert the following code:

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"
lang="en"
metal:use-macro="here/main_template/macros/master"
i18n:domain="plone">

<body>

<div metal:fill-slot="main">

<h1>Add New User</h1>

<tal:comment tal:replace="nothing">
Submits the form's data to the python script insertusers. insertusers is where
the transaction with the MySQL database actually takes place.
</tal:comment>

<form name="insertusers" action="insertusers" method="post">

<fieldset>

<legend>User Details</legend>

<div class="field">
<label>First Name</label>
<p><input size="60" type="text" name="first_name"></p>
</div>

<div class="field">
<label>Last Name</label>
<p><input size="60" type="text" name="last_name"></p>
</div>

</fieldset>

<div class="formControls">
<input type="submit" value="Submit">
</div>

</form>
</div>

</body>
</html>

With this form, the values first_name and last_name are submitted to the Python script insertusers which we will create next.


The Python Script

To add a Python script, select “Script (Python)” from the drop down menu located at the top right corner of the ZMI.


pythonscript.gif


Under the id field of the Python script enter insertusers.


Insert the following code:

#Import a standard function, and get the HTML request and response objects.
from Products.PythonScripts.standard import html_quote
request = container.REQUEST
RESPONSE = request.RESPONSE

# Insert data that was passed from the form
context.insert_users(first_name=request.first_name, last_name=request.last_name)

# Re-direct back to your location
RESPONSE.redirect('/wherever/your/page/is/located')

The values first_name and last_name that were submitted by the page template form addnames to this Python script are retrieved via REQUEST objects and then inserted into the MySQL database using a Z SQL Method called insert_users().

You may also want to validate the data before it is inserted into your database. For this, you can use Controller Page Templates or create your own validation methods within this Python Script before the insert statement. However, if you use Controller Page Templates you’ll get all the nice bells and whistles that Plone comes with regarding error feedback, such as the orange boxes that appear when a field is missing on a form submission.


Z SQL Method

The Z SQL method that we use for inserting values into the MySQL database contains the two values first_name and last_name which are passed as arguments. dtml-sqlvar is used when passing arguments from the Z SQL Methods to your MySQL database. The Arguments must be the same name as the dtml-sqlvar values.

insert_users.gif