In regular HTML, you can have multiple fields POST to an array:
<input type="text" name="arr[]">
<input type="text" name="arr[]">
<input type="text" name="arr[]">
<input type="text" name="arr[]">
How can I get this functionality from WTForms? Basically, I have a form where users and click on little plus and minus buttons to add or remove fields from the form.
You're looking for WTForm FieldList. It allows you to create an arbitrary list of the same field.
Ex.
emails = FieldList(StringField('email'), min_entries=1, max_entries=5)
Related
In the website I'm building with Flask I'm using WTForms to validate submitted forms. In one form, I'm submitting a list of values with a form like this (the hidden fields are generated client side using js):
<form action="" method="post" id="prop-form">
<input type="hidden" name="ids[]" value="54511ea9c1a36b4e910ce52a">
<input type="hidden" name="ids[]" value="54511ea9c1a36b4e910ce52d">
<input id="title" name="title" size="30" type="text" value="">
<input type="submit" value="Save this form">
</form>
On the Flask side, I can easily get the contents of ids[] using he following code:
print request.form
print request.form.getlist('ids[]')
prints out:
ImmutableMultiDict([('ids[]', u'54511ea9c1a36b4e910ce52a'), ('ids[]', u'54511ea9c1a36b4e910ce52d'), ('title', u'Blablabla')])
[u'54511ea9c1a36b4e910ce52a', u'54511ea9c1a36b4e910ce52d']
But when I create a SelectMultipleField in my wtform as follows:
ids = SelectMultipleField('ids[]')
and then try to get those values, I get an empty list:
print form.ids.data # prints out []
Any idea how I could mimic the behaviour of request.form.getlist('ids[]') using WTForms? All tips are welcome!
I'm a new pie to django and trying to use forms.CharField to generate a required input.
By default, a forms.CharField is translated as:
<input id="id_mail_to" name="mail_to" type="text" />
But I want a:
<input id="id_mail_to" name="mail_to" type="text" required/>
How can I get that?
Use attrs
Try this,
mail_to=forms.CharField(required=True, widget=forms.TextInput(attrs={'required': "required"}))
You can see this,
<input id="id_mail_to" name="mail_to" required="required" type="text">
JsFiddle
You can try like this in forms.py
mail_to= forms.CharField(required=True, label="mail to")
i am doing a small project in django by using python.
In that project, there is a table with list of pending task having columns: task_id,summary,description,due_date.
I have to give 'edit' button in each row so that one can edit the data of that particular row. But I am not able to identify the button click event in view page for the particular row.
Please somebody help me...
I'm assuming you're not talking about browser click events since you're tagging this with python and django only.
You can identify which <input type="submit"> element was pressed by giving it a name.
A user clicking on <input type="submit" value="edit" name="summary" /> will be identified by the presence of a summary key in request.POST
# form snippet
<input type="submit" value="Edit Summary" name="summary" />
<input type="submit" value="Edit Due Date" name="due_date" />
# view snippet
if request.POST.get('summary'):
print('user clicked summary')
elif request.POST.get('due_date'):
print('user clicked due date')
Below is my answer (an example) if you want to use "GET" instead of "POST".
Assuming that you want to get a value from the user input in html textbox whenever the user clicks 'edit' button, and then call a python function (mypythonfunction) that you wrote inside mypythoncode.py. Note that "btn" class is defined in a css file.
inside yourtemplate.html:
<form action="#" method="get">
<input type="text" value="8" name="mytextbox" size="1"/>
<input type="submit" class="btn" value="edit" name="mybtn">
</form>
inside view.py:
import mypythoncode
def mymethod(request):
if(request.GET.get('mybtn')):
mypythoncode.mypythonfunction( int(request.GET.get('mytextbox')) )
return render_to_response('App/yourtemplate.html')
I'm two days in to Python and GAE, thanks in advance for the help.
I have an input array in HTML like this:
<input type="text" name="p_item[]">
<input type="text" name="p_item[]">
<input type="text" name="p_item[]">
I want to parse the input in Python, and I'm trying this, which isn't working:
items = self.request.get('p_item')
for n in range(1,len(items)):
self.response.out.write('Item '+n+': '+items[n])
What is the correct way to do this?
Change your html to this
<input type="text" name="p_item">
<input type="text" name="p_item">
<input type="text" name="p_item">
and use the self.request.get_all() method http://code.google.com/appengine/docs/python/tools/webapp/requestclass.html#Request_get_all
p.s. For reference, there is no concept of arrays for GET/POST data, your form gets transformed a key=value string separated by '&' e.g.
p_item=1&p_item=3&p_item=15
etc, it's up to the web framework to interpret whether a parameter is an array.
Edit: oops, just read the comments that you figured this out already, oh well :P
I would recommend doing some debugging if this sort of issue comes up. Make things simple and write out your variable values and ensure you get what you expect at each step. Do something like the following:
<form method="get">
<input type="text" name="single_key" />
<input type="text" name="array_key[some_key]" />
<input type="submit" />
</form>
And see what happens when running the following Python on the backend:
single_value = self.request.get('single_key')
self.response.out.write(str(single_value))
array_value = self.request.get('array_key')
self.response.out.write(str(array_value))
Based on the output you should have a better idea of what to get the desired results or how to add more detail to your question if you still don't understand a certain behavior.
Is there any way for Pyramid to process HTML form input which looks like this:
<input type="text" name="someinput[]" value="" />
or even more usefully:
<input type="text" name="someinput[0][subelement1]" value="" />
<input type="text" name="someinput[0][subelement2]" value="" />
<input type="text" name="someinput[1][subelement1]" value="" />
<input type="text" name="someinput[1][subelement2]" value="" />
...and access that data easily (e.g. via a dict)?
Any help would be much appreciated!
EDIT: to make it clearer, what I need is the ability to have a form where a user can add as many 'instances' of a group of input elements, e.g. adding between 1 and n users, each containing a firstname, lastname, username (or something like that).
One solution would be to use peppercorn. Although it does not support the syntax you're looking for, it will let you send structured data to your Pyramid application through the use of forms. A more casual description exists too.