Using Spyce - Handling Form Data
(Page 4 of 4 )
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('<', '<')]]</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.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |