Reputation: 2735
I'm using the cgi
module to handle form input data, but I would like to have the form and the processing in separate files (modules?). Is there a way to pass the POST
ed information from the HTML to the processing file?
I'm not using any frameworks and would like to keep this as simple as possible. I know that I can get this to work by having the HTML printed in the python file, but I'd like to keep the two separate.
Here's the real problem code:
<form action="processes/process.py" method="post">
<div id="name1" class="formData"><label for="">First name:</label><input name="firstName" type="text" id="firstName" /></div>
<div id="name2" class="formData"><label>Last name:</label><input name="lastName" type="text" id="lastName" /></div>
<div id="email" class="formData"><label>Email address:</label><input name="emailAddress" type="text" id="emailAddress" /></div>
<div id="mailAddress" class="formData"><label>Mailing address:</label><input name="physicalAddress" type="textarea" id="physicalAddress" /></div>
<input type="submit" value="Submit" onClick="T_validateForm('firstName','','R','lastName','','R','EmailAddress','','RisEmail');return document.T_returnValue">
</form>
I can have this printed in my process.py file and change the action, but I would like this form code to be separate from the code I will use to manage and distribute the information form the form. I need to pass the data to process.py
I'm trying to get the data in the python code here:
form = cgi.FieldStorage()
firstName = form['firstName'].value
lastName = form['lastName'].value
emailAddress = form['emailAddress'].value
physicalAddress = form['physicalAddress'].value
I know that in php form
is replaced by $_POST
. Is there a similar identifier in python? form
works when they're in the same file, but it seems that there should be some way to tell python to get the data from the HTML POST
. I thought that cgi.FieldStorage()
did that, but it doesn't work. Should I be passing parameters to cgi.FieldStorage()
?
Upvotes: 1
Views: 1839
Reputation: 32650
The question is pretty broad, altough I understand separating content, logic and presentation is what you're after.
Are you maybe looking for a templating language like Jinja, Mako or Chameleon? Or a simple form generation and validation library like FormEncode? Those can be just as easily used with or without a web framework.
As opposed to PHP, in Python CGI is hardly ever used. We use WSGI instead, or web frameworks (micro to large) with their own request model and form handling libraries. Doing it yourself with CGI could be an educational example, but you'll almost always be better of using an existing framework even for small web apps. Could be a micro framework like bottle.
If it's unclear to you how to factor out code into several Python modules, I would ask a new question about that (probably best with no ties to web development whatsoever). Additionally, I'd evaluate a couple templating languages for Python, and possibly a few micro frameworks.
Upvotes: 2