Multiple Forms of the same Class
- From:
- Christoph Blau
- Date:
- 2014-07-29 @ 13:11
Hey Guys
I need some help with submitting and validating my form. I have a page
that displays the same WTForm multiple times and I cannot get it to
submit.
I came across a post where the suggestion was to add a prefix to the form.
This I have tried but the problem is that it is that same prefix for all
form.
So how can I distinguish between the forms to submit them separately? I am
wondering if there is a pythonic / flask way to do it or if I have to use
jQuery or something to that effect. Any advise is much appreciated.
Here my view:
@app.route('/domains/<domain>', methods = ['GET', 'POST'])
def dnsDisplay(domain=None):
name='Zone Management'
domain = domain or request.args.get('domain')
title = 'Domains'
Records = getRecords(domain)
aRecords=[]
for sublist in Records:
if sublist[0] == 'A':
sub={
'type':sublist[0],
'record':sublist[1],
'ttl':sublist[2],
'content':sublist[3]
}
aRecords.append(sub)
aFrm = aForm(request.form,prefix='ARec')
if aFrm.validate_on_submit() and aFrm.submit.data:
print aFrm.data
return
render_template('selected.html',name=domain,title=title,aFrm=aFrm,aRec=aRecords)
the template:
{% extends 'base.html' %}
{%block main%}
{% for message in aFrm.errors %}
<div id=flashes>
{{ message }}
</div>
{% endfor %}
<fieldset id=shadow>
<legend>A RECORDS</legend>
{%for a in aRec%}
<form method=POST action=. name={{a.record}}>
{{aFrm.hidden_tag()}}
{{aFrm.type(size=2,value=a.type)}}
{{aFrm.record(size=15,value=a.record)}}
{{aFrm.ttl(size=5,value=a.ttl)}}
{{aFrm.content(size=15,value=a.content)}}
{{aFrm.edit}}
</form>
{%endfor%}
</fieldset>
{%endblock%}
and the form:
class aForm(Form):
type = HiddenField('Type')
regex = re.compile('^[a-zA-Z0-9-]+$')
record = TextField('Hostname', validators=[Required(),Regexp(regex)])
ttl = TextField('TTL',
validators=[Required(),NumberRange(min=60,max=84600)])
content = TextField('Destination', validators=[Required(),IPAddress()])
edit = SubmitField('Update')