librelist archives

« back to archive

little wizard with forms ¿?

little wizard with forms ¿?

From:
Bernat
Date:
2012-09-18 @ 07:17
Hi all,

I'm new to Flask and I'm trying to create a little wizard with a form, 
but I need some fields use them globally in the wizard and I try to do 
with the 'g', but it doesn't work. I have seen that 'g' is only working 
for a request.

I have cerate the model with some fields with SQLAlchemy.


I have created a form.py like:

class FormA(Form):
     fieldA = TextField('fieldA', [Required()])
     fieldB = TextField('fieldB', [Required()])


I have create a views.py like:

@app.route('/index', methods=['GET', 'POST'])
def index():
     form = FormA(request.form)
     step = 1
     if request.method == 'POST':
         if form.validate():
             step = 2
             save the 2 fields in db and return an OK message
         else:
             if form.fieldA.data:
                 take that field
             else:
                 flash('Something is wrong')
             step = 2
     return render_template('form.html', form=form, step=step)


I have create the form.html like:

{% extends "base.html" %}

{% block content %}
     {% if step == 1 %}
<div>FieldA: {{ form.fieldA }}</div>
     {% elif step == 2 %}
<div>print the fieldA, taken the before step</div>
<div>FieldB: {{ form.fieldB }}</div>
     {% else %}
<div>Something is wrong</div>
     {% endif %}


I know that this is simple and there are some bugs to resolve, specialy 
for the step change, but by the moment I will need to know it that it's 
the correct way or may be I'm in the wrong way to do that? Any idea?

Thanks a lot!

-- 
bernat

Re: [flask] little wizard with forms ¿?

From:
Mark Steve Samson
Date:
2012-09-18 @ 07:23
Use session. :) 

-- 
Steve


On Tuesday, September 18, 2012 at 3:17 PM, Bernat wrote:

> Hi all,
> 
> I'm new to Flask and I'm trying to create a little wizard with a form, 
> but I need some fields use them globally in the wizard and I try to do 
> with the 'g', but it doesn't work. I have seen that 'g' is only working 
> for a request.
> 
> I have cerate the model with some fields with SQLAlchemy.
> 
> 
> I have created a form.py like:
> 
> class FormA(Form):
> fieldA = TextField('fieldA', [Required()])
> fieldB = TextField('fieldB', [Required()])
> 
> 
> I have create a views.py like:
> 
> @app.route('/index', methods=['GET', 'POST'])
> def index():
> form = FormA(request.form)
> step = 1
> if request.method == 'POST':
> if form.validate():
> step = 2
> save the 2 fields in db and return an OK message
> else:
> if form.fieldA.data:
> take that field
> else:
> flash('Something is wrong')
> step = 2
> return render_template('form.html', form=form, step=step)
> 
> 
> I have create the form.html like:
> 
> {% extends "base.html" %}
> 
> {% block content %}
> {% if step == 1 %}
> <div>FieldA: {{ form.fieldA }}</div>
> {% elif step == 2 %}
> <div>print the fieldA, taken the before step</div>
> <div>FieldB: {{ form.fieldB }}</div>
> {% else %}
> <div>Something is wrong</div>
> {% endif %}
> 
> 
> I know that this is simple and there are some bugs to resolve, specialy 
> for the step change, but by the moment I will need to know it that it's 
> the correct way or may be I'm in the wrong way to do that? Any idea?
> 
> Thanks a lot!
> 
> -- 
> bernat
> 
> 

Re: [flask] little wizard with forms ¿?

From:
Bernat
Date:
2012-09-18 @ 19:07
Al 18/09/12 09:23, En/na Mark Steve Samson ha escrit:
> Use session. :)

Oh! shit, that's perfect!

I don't know where I was looking for before. :-)

Thanks

>
> -- 
> Steve
>
> On Tuesday, September 18, 2012 at 3:17 PM, Bernat wrote:
>
>> Hi all,
>>
>> I'm new to Flask and I'm trying to create a little wizard with a form,
>> but I need some fields use them globally in the wizard and I try to do
>> with the 'g', but it doesn't work. I have seen that 'g' is only working
>> for a request.
>>
>> I have cerate the model with some fields with SQLAlchemy.
>>
>>
>> I have created a form.py like:
>>
>> class FormA(Form):
>> fieldA = TextField('fieldA', [Required()])
>> fieldB = TextField('fieldB', [Required()])
>>
>>
>> I have create a views.py like:
>>
>> @app.route('/index', methods=['GET', 'POST'])
>> def index():
>> form = FormA(request.form)
>> step = 1
>> if request.method == 'POST':
>> if form.validate():
>> ste p = 2
>> save the 2 fields in db and return an OK message
>> else:
>> if form.fieldA.data:
>> take that field
>> else:
>> flash('Something is wrong')
>> step = 2
>> return render_template('form.html', form=form, step=step)
>>
>>
>> I have create the form.html like:
>>
>> {% extends "base.html" %}
>>
>> {% block content %}
>> {% if step == 1 %}
>> <div>FieldA: {{ form.fieldA }}</div>
>> {% elif step == 2 %}
>> <div>print the fieldA, taken the before step</div>
>> <div>FieldB: {{ form.fieldB }}</div>
>> {% else %}
>> <div>Something is wrong</div>
>> {% endif %}
>>
>>
>> I know that this is simple and the re are some bugs to resolve, specialy
>> for the step change, but by the moment I will need to know it that it's
>> the correct way or may be I'm in the wrong way to do that? Any idea?
>>
>> Thanks a lot!
>>
>> -- 
>> bernat
>


-- 
bernat

Re: [flask] little wizard with forms ¿?

From:
Matt Good
Date:
2012-09-18 @ 21:06
One caveat with storing the form state in the session is that it's global 
for the browser, so if the user happens to have the page open in multiple 
tabs, they'll share the same state.

Another approach is to keep passing along the values from all previous 
pages in a set of hidden fields.

You might also ask on the mailing list for WTForms or whatever form 
library you're using.


On Sep 18, 2012, at 12:07 PM, Bernat <cow@cowdot.com> wrote:

> Al 18/09/12 09:23, En/na Mark Steve Samson ha escrit:
>> Use session. :)
> 
> Oh! shit, that's perfect!
> 
> I don't know where I was looking for before. :-)
> 
> Thanks
> 
>> 
>> -- 
>> Steve
>> 
>> On Tuesday, September 18, 2012 at 3:17 PM, Bernat wrote:
>> 
>>> Hi all,
>>> 
>>> I'm new to Flask and I'm trying to create a little wizard with a form,
>>> but I need some fields use them globally in the wizard and I try to do
>>> with the 'g', but it doesn't work. I have seen that 'g' is only working
>>> for a request.
>>> 
>>> I have cerate the model with some fields with SQLAlchemy.
>>> 
>>> 
>>> I have created a form.py like:
>>> 
>>> class FormA(Form):
>>> fieldA = TextField('fieldA', [Required()])
>>> fieldB = TextField('fieldB', [Required()])
>>> 
>>> 
>>> I have create a views.py like:
>>> 
>>> @app.route('/index', methods=['GET', 'POST'])
>>> def index():
>>> form = FormA(request.form)
>>> step = 1
>>> if request.method == 'POST':
>>> if form.validate():
>>> ste p = 2
>>> save the 2 fields in db and return an OK message
>>> else:
>>> if form.fieldA.data:
>>> take that field
>>> else:
>>> flash('Something is wrong')
>>> step = 2
>>> return render_template('form.html', form=form, step=step)
>>> 
>>> 
>>> I have create the form.html like:
>>> 
>>> {% extends "base.html" %}
>>> 
>>> {% block content %}
>>> {% if step == 1 %}
>>> <div>FieldA: {{ form.fieldA }}</div>
>>> {% elif step == 2 %}
>>> <div>print the fieldA, taken the before step</div>
>>> <div>FieldB: {{ form.fieldB }}</div>
>>> {% else %}
>>> <div>Something is wrong</div>
>>> {% endif %}
>>> 
>>> 
>>> I know that this is simple and the re are some bugs to resolve, specialy
>>> for the step change, but by the moment I will need to know it that it's
>>> the correct way or may be I'm in the wrong way to do that? Any idea?
>>> 
>>> Thanks a lot!
>>> 
>>> -- 
>>> bernat
>> 
> 
> 
> -- 
> bernat
> 

Re: [flask] little wizard with forms ¿?

From:
Bernat
Date:
2012-09-19 @ 08:27
Al 18/09/12 23:06, En/na Matt Good ha escrit:
> One caveat with storing the form state in the session is that it's 
global for the browser, so if the user happens to have the page open in 
multiple tabs, they'll share the same state.

I know it, thanks. But for the think I need it, it's enough. It's a 
little register form and could be sufficient.

> Another approach is to keep passing along the values from all previous 
pages in a set of hidden fields.

Could be a good solution. I will try it too.

> You might also ask on the mailing list for WTForms or whatever form 
library you're using.

It's WTForms. I will ask there too.

Thanks!

bernat

>
>
> On Sep 18, 2012, at 12:07 PM, Bernat<cow@cowdot.com>  wrote:
>
>> Al 18/09/12 09:23, En/na Mark Steve Samson ha escrit:
>>> Use session. :)
>> Oh! shit, that's perfect!
>>
>> I don't know where I was looking for before. :-)
>>
>> Thanks
>>
>>> -- 
>>> Steve
>>>
>>> On Tuesday, September 18, 2012 at 3:17 PM, Bernat wrote:
>>>
>>>> Hi all,
>>>>
>>>> I'm new to Flask and I'm trying to create a little wizard with a form,
>>>> but I need some fields use them globally in the wizard and I try to do
>>>> with the 'g', but it doesn't work. I have seen that 'g' is only working
>>>> for a request.
>>>>
>>>> I have cerate the model with some fields with SQLAlchemy.
>>>>
>>>>
>>>> I have created a form.py like:
>>>>
>>>> class FormA(Form):
>>>> fieldA = TextField('fieldA', [Required()])
>>>> fieldB = TextField('fieldB', [Required()])
>>>>
>>>>
>>>> I have create a views.py like:
>>>>
>>>> @app.route('/index', methods=['GET', 'POST'])
>>>> def index():
>>>> form = FormA(request.form)
>>>> step = 1
>>>> if request.method == 'POST':
>>>> if form.validate():
>>>> ste p = 2
>>>> save the 2 fields in db and return an OK message
>>>> else:
>>>> if form.fieldA.data:
>>>> take that field
>>>> else:
>>>> flash('Something is wrong')
>>>> step = 2
>>>> return render_template('form.html', form=form, step=step)
>>>>
>>>>
>>>> I have create the form.html like:
>>>>
>>>> {% extends "base.html" %}
>>>>
>>>> {% block content %}
>>>> {% if step == 1 %}
>>>> <div>FieldA: {{ form.fieldA }}</div>
>>>> {% elif step == 2 %}
>>>> <div>print the fieldA, taken the before step</div>
>>>> <div>FieldB: {{ form.fieldB }}</div>
>>>> {% else %}
>>>> <div>Something is wrong</div>
>>>> {% endif %}
>>>>
>>>>
>>>> I know that this is simple and the re are some bugs to resolve, specialy
>>>> for the step change, but by the moment I will need to know it that it's
>>>> the correct way or may be I'm in the wrong way to do that? Any idea?
>>>>
>>>> Thanks a lot!
>>>>
>>>> -- 
>>>> bernat
>>
>> -- 
>> bernat
>>
>


-- 
bernat