Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #43822

Re: dynamic forms generation

Date 2013-04-18 07:53 -0500
From Wayne Werner <wayne@waynewerner.com>
Subject Re: dynamic forms generation
References <CAF_E5Ja93v_6-AcTc=T2EVOUPufYk12zmiv24QRTPKFetfAfcg@mail.gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.765.1366289645.3114.python-list@python.org> (permalink)

Show all headers | View raw


[Multipart message — attachments visible in raw view] - view raw

On Tue, 16 Apr 2013, andrea crotti wrote:

> This is not really scalable, and we want to make the whole thing more
> generic.
> 
> So ideally there could be a DSL (YAML or something else) that we could
> define to then generate the forms, but the problem is that I'm quite
> sure that this DSL would soon become too complex and inadeguate, so I'm
> not sure if it's worth since noone should write forms by hands anyway.
> 
> Between the things that we should be able to do there are:
> - dependent fields
> - validation (both server and client side, better if client-side
>   auto-generated)
> - following DRY as much as possible
> 
> Any suggestions of possible designs or things I can look at?

I would highly recommend a look at Flask, and Flask-WTF in particular. 
It's fairly easy to write forms, and with only a bit of setup you can end 
out with some fairly generic systems.

I don't think that by default it does any client-side validation 
generation, but as the HTML for the forms are completely generated, 
extending the form and adding validation logic to the output wouldn't be 
too difficult.

Example:

# form.py

from flask.ext.wtf import Form, TextField, Required

class MyBasicForm(Form):
     some_text = TextField("Put some text here:", validators=[Required()])


# View/HTML

{% extends 'base.html' %}
{{ form.some_text.label() }}{{ form.some_text(size=40) }}


# Server code

@app.route("/basic_form", methods=['GET', 'POST'])
def basic():
     form = MyBasicForm()
     if form.validate_on_submit():
         do_the_needful(form.some_text.data)
 	return redirect(url_for('main'))

     return render_template('basic_form.html', form=form)



Obviously a really basic example. Check out Flask here:
http://flask.pocoo.org/

And Flask WTF here:
http://pythonhosted.org/Flask-WTF/


HTH,
Wayne

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: dynamic forms generation Wayne Werner <wayne@waynewerner.com> - 2013-04-18 07:53 -0500

csiph-web