The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
filling in a form
Discuss filling in a form in the Python Programming forum on Dev Shed. filling in a form Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

February 3rd, 2004, 03:00 PM
|
|
Registered User
|
|
Join Date: Jan 2004
Posts: 11
Time spent in forums: 13 m 41 sec
Reputation Power: 0
|
|
|
filling in a form
Is it possible to repopulate a form using Pyton?
After the user has pressed the SUBMIT button, I save the forms data into a dictionary and then validate the input. If errors are found or no data was entered, the apprpriate error message(s) are displayed and a blank form is shown under the error messages. I can't figure out how to set the 'value' property to the previously entered value.
For example in PHP I would write
PHP Code:
<b>user name:</b><input type=\"text\" name=\"user_name\" value=\"<? echo stored_user;?>\"></br>
where "stored_user" is either blank (no value entered or theres an error with the value entered) or the previous entered data.
Thanks for any help.
|

February 3rd, 2004, 04:06 PM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
Yeah its very possible, and pretty easy; heres you're PHP code. However since this is CGI you cant embed Python in HTML but hey...
Code:
print '<b>user name:</b><input type="text" name="user_name" value="%s"></br>' % 'stored_user'
You can use the cgi module to get form data, then just output the form with the appropriate data.
http://www.python.org/doc/2.3.3/lib/module-cgi.html
Edit: added a print statment to the beginning of the code block to make it more understandable...
Mark.
__________________
programming language development: www.netytan.com – Hula
Last edited by netytan : February 24th, 2004 at 07:14 PM.
|

February 24th, 2004, 06:57 PM
|
|
Registered User
|
|
Join Date: Jan 2004
Posts: 11
Time spent in forums: 13 m 41 sec
Reputation Power: 0
|
|
|
I'm writing the script in Python, not PHP. I was just using PHP to show what I was trying to do as I'm just learning Python.
|

February 24th, 2004, 07:08 PM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
I'm fully aware of why you posted the PHP code dude  . And if you give it a go you'll find that this is exactly what you wanted... a pythonic version of you're PHP code
Good luck with the project,
Mark.
Last edited by netytan : February 24th, 2004 at 07:13 PM.
|

February 25th, 2004, 09:06 AM
|
|
Contributing User
|
|
Join Date: May 2003
Location: Norway
Posts: 41
Time spent in forums: 3 h 52 m 22 sec
Reputation Power: 10
|
|
I just discovered FormEncode, which is supposed to do what you need and then some. http://formencode.org/
Maybe a little complicated for simple form though.
I am also working on my own form module to take care of form input and output. I have a form base class that implements common functionality for forms and classes for describing the different form controls defined by html.
One example is the Textarea class which implements a textarea  :
Code:
class Textarea(SingleItem):
def __init__(self, name, rows, cols, value=""):
SingleItem.__init__(self, name, value)
self.rows = rows
self.cols = cols
def output(self):
return """<textarea name="%s" rows="%s" cols="%s">
%s
</textarea>"""%(self.name, self.rows, self.cols, self.value)
The SingleItem class ((base class for form controls with only one option) and it's parent, FormItem(base class for all form controls)) takes care of setting the name and the value of the form control. It might look like a lot of work for a single form, but I am doing this to have some code I can just plug in to any Python web application.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|