I need to send some HTML form data to a python script for formatting in a web2py application but don’t know how to do this. The user enters the data into a HTML form. The data is sent to a script where it is cleaned up and tokenized. The revised data is sent to another python function where it is to be used. How do I go about this? Does every python function go into the default.py controller or should I create a new .py file to process the form data? I have an example here. This takes the search term as input by the user and sends it to the results page. How would I send it to a python script before sending it to the results page? I’m really confused by this!
index.html:
<div id="MainArea">
<p align="center">MY SEARCH ENGINE</p>
<form name="form1" method="get" action="results.html">
<label for="SearchBar"></label>
<div align="center">
<input name="query" type="text" id="SearchBar" value="" size = "100px"><br />
<input name="submit" type="submit" value="Search">
</div>
</form>
<p align="center"> </p>
</div>
results.html
{{extend 'layout.html'}}
<h1>This is the default/results.html template</h1>
{{=BEAUTIFY(response._vars)}}
<div>{{=results}}</div>
default.py:
import urllib2
def index():
return dict()
def results():
address = "http://www.blekko.com/?q=%(query)s+/json&auth=<mykey>" % dict(query=request.vars.query)
response = urllib2.urlopen(address)
html=response.read()
return html
It doesn’t look like you are using the built-in form mechanisms. I am curious — how exactly do you need to clean up your data?
Normally, you would create a controller file with a function that builds the form and also has a branch to process it. In your data model, you would specify what is acceptable input, and in the form processing function (controller function) you would manipulate the data.
You can also use
Where onvalidation is a function that can further process your form, create/manipulate values before they are stored in the DB (or you can not store them in DB at all), and do further validations.
See: http://web2py.com/books/default/chapter/29/7#onvalidation
Web2py forms generally self-submit and have a session key set to prevent double submissions and the like. The way you’re doing it you would have to make sure to use form.accepts(…) without session or skip the web2py form.accepts/form.process and just use request.vars.field_name_that_you_provided to get the form field data. Then you would have to validate the data yourself.
I would recommend creating a web2py model (models/form.py), or at the very least creating the form using web2py’s FORM helper inside the controller file that is responsible for displaying and processing your form.
This is the web2py way — a single function that either returns a form to fill in or processes it. You can use redirection to send them to another page if everything is groovy.
another way is this:
The redirect will send the validated form data onto the next function. If you don’t need to send it to a web2py function, then execute whatever function is required from the process_form function.
Make sense?
EDIT: Another way of saying this is that the script contents should execute in the form processing function. You can define a controller function that is explicitly executed by another function in the same controller file. It would not have a view associated and would not be a standalone page, but just a function.
You can also use execfile(“path/to/file”). I would recommend putting the script in applications//private/ and using request.folder to get the folder the app lives in, then just tack on private…so something like:
But you’ll have to verify that gets you the proper path.
EDIT (Validating Forms): Check out the web2py book on how to make data models and use forms/validators ( http://web2py.com/books/default/chapter/29/7 ). When you define a web2py data model, you can include validators directly on the field definition. This way, the database will know if it’s an integer or string, and you can force lengths or regex patterns on the input. The advantage is that the form you create with:
or even:
form = SQLFORM.factory(Field(‘some_field’, ‘integer’), Field(‘some_str’, ‘string’, requires=IS_ALPHANUMERIC))
The validation is very easy — the integer typed fields will immediately delete any non-numeric values entered, and even if you modified the source code or sent a POST/GET request with bad values, the form.process(…) method would invalidate it and allow you to easily highlight the errors.
Here’s an example form definition:
Which will create a table called news_item in a database defined earlier in the program (db = DAL(“…connection string”) ), and allow you to create a good-looking form instantly. Notice there are many option paramaters.
You can also skip the DB and make forms directly, but I’ll let you look that up in the docs 🙂