Home arrow Python arrow Page 4 - Using Spyce

Handling Form Data - Python

If you are looking for a way to embed Python within HTML you might want to take a look at a web environment called Spyce. Two of its advantages are its features and its flexibility. Keep reading to learn how you can Spyce up your programs.

TABLE OF CONTENTS:
  1. Using Spyce
  2. Basic Syntax
  3. Active Tags
  4. Handling Form Data
By: Peyton McCullough
Rating: starstarstarstarstar / 9
January 17, 2006

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Spyce contains a few methods that deal with handling form data. You've already seen one of them briefly, but let's take a look at some more of them:

[[\
import random
methods = [request.get, request.get1, request.post, request.post1,\
           request.getpost, request.getpost1, request.postget, request.postget1]
genList = [[spy! method:
   <b>[[=str(method).replace('<', '&lt;')]]</b><br />
   [[ for field in ('A','B','C'): { ]]
      [[=field]]: [[=method(field)]]<br />
   [[ } ]]
   <br />]]
]]

[[-- Handle processing --]]
[[ if request.getpost1('A', None): { ]]
   [[ for method in methods: { ]]
      [[ genList(method) ]]
   [[ } ]]
[[ } ]]

[[-- The actual form --]]
<form method='[[=random.choice(['POST', 'GET'])]]'>
A: <input type='text' name='A' /><br />
B: <input type='text' name='B' /><br />
C: <input type='text' name='C' /><br />
<input type='submit' value='Submit' />
</form>

The script above sends form data through either the “GET” or “POST” methods randomly, and then it displays the results of several of Spyce's methods, which are listed in the methods list. Below that, we define getList. Notice, however, that getList deals with Spyce itself. The “[[!spy X]]” structure simply defines a function that can be accessed later. In this case, it's a function that calls a method with either “A”, “B” or “C” as an argument—our field names. Later in the script, we call this function with each item in methods. Observe what each method returns, and observe what happens when “GET” and “POST” data are mixed.

Conclusion

Spyce has a lot to offer to web developers and developers looking for dynamic content in general. One of its main advantages is that it can work alone, with Apache, with mod_python, with FastCGI or with CGI without any major modifications. If one method is not available to you, there's always more to fall back on. It offers several tags that can be used for unique purposes, and it offers a way for developers to save time by using active tags. Of course, there are also plenty of others features of Spyce that cannot be covered in a single article. Spyce isn't the only way of embedding Python code, but it is arguably the most feature-filled way.



 
 
>>> More Python Articles          >>> More By Peyton McCullough
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PYTHON ARTICLES

- Python Big Data Company Gets DARPA Funding
- Python 32 Now Available
- Final Alpha for Python 3.2 is Released
- Python 3.1: String Formatting
- Python 3.1: Strings and Quotes
- Python 3.1: Programming Basics and Strings
- Tuples and Other Python Object Types
- The Dictionary Python Object Type
- String and List Python Object Types
- Introducing Python Object Types
- Mobile Programming using PyS60: Advanced UI ...
- Nested Functions in Python
- Python Parameters, Functions and Arguments
- Python Statements and Functions
- Statements and Iterators in Python

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: