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


Groups > comp.lang.python > #43822 > unrolled thread

Re: dynamic forms generation

Started byWayne Werner <wayne@waynewerner.com>
First post2013-04-18 07:53 -0500
Last post2013-04-18 07:53 -0500
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

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

#43822 — Re: dynamic forms generation

FromWayne Werner <wayne@waynewerner.com>
Date2013-04-18 07:53 -0500
SubjectRe: dynamic forms generation
Message-ID<mailman.765.1366289645.3114.python-list@python.org>

[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

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web