February 3rd, 2004, 04:00 PM
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
where "stored_user" is either blank (no value entered or theres an error with the value entered) or the previous entered data.
<b>user name:</b><input type=\"text\" name=\"user_name\" value=\"<? echo stored_user;?>\"></br>
Thanks for any help.
February 3rd, 2004, 05:06 PM
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...
You can use the cgi module to get form data, then just output the form with the appropriate data.
print '<b>user name:</b><input type="text" name="user_name" value="%s"></br>' % 'stored_user'
Edit: added a print statment to the beginning of the code block to make it more understandable...
Last edited by netytan; February 24th, 2004 at 08:14 PM.
February 24th, 2004, 07:57 PM
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, 08:08 PM
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,
Last edited by netytan; February 24th, 2004 at 08:13 PM.
February 25th, 2004, 10:06 AM
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 :
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.
def __init__(self, name, rows, cols, value=""):
SingleItem.__init__(self, name, value)
self.rows = rows
self.cols = cols
return """<textarea name="%s" rows="%s" cols="%s">
</textarea>"""%(self.name, self.rows, self.cols, self.value)