librelist archives

« back to archive

Customize Flask WTF Forms

Customize Flask WTF Forms

From:
peg kh
Date:
2014-12-19 @ 19:50
I have a app that display input of forms Now I want to give a option to
user to *edit the wtf-forms* How can I do that?


app.py

app.route('/info', methods=['GET', 'POST'])def info():
  form = Form()
  if request.method == 'POST':
     if form.validate()== True:
     info = Contacts(name=form.name.data,
                                  email=form.email.data,
                                  age=form.age.data
                                 )
      db.session.add(info)
      db.session.commit()
      #flash('Contact commited to database')
      return redirect(url_for('view'))

     else: #If the form does not have all fields that are required
            flash('All fields are required.')
  return render_template('info.html', form=form)
@app.route('/view', methods=['GET', 'POST'])def info():
  viewinfo = Contacts.query.all()

  return render_template('view.html', view = viewinfo)
@app.route('/edit<id>', methods=['GET', 'POST'])def edit(id):# How can
I edit the forms without Flask-Admin??????

Re: [flask] Customize Flask WTF Forms

From:
digwtx
Date:
2014-12-20 @ 13:18
The wtforms' Form class has a `populate_obj` method.

One common usage of this is an edit profile view:

```python
def edit_profile(request):
user = User.objects.get(pk=request.session['userid'])
form = EditProfileForm(request.POST, obj=user)

if request.POST and form.validate():
form.populate_obj(user)
user.save()
return redirect('/home')
return render_to_response('edit_profile.html', form=form)
```

read the docs for more details.

At 2014-12-20 03:50:30, "peg kh" <peg1988.kh@gmail.com> wrote:

|
|

I have a app that display input of forms Now I want to give a option to 
user to edit the wtf-forms How can I do that?





app.py

app.route('/info', methods=['GET','POST'])def info():
  form =Form()if request.method =='POST':if form.validate()==True:
     info =Contacts(name=form.name.data,
                                  email=form.email.data,
                                  age=form.age.data
                                 )
      db.session.add(info)
      db.session.commit()#flash('Contact commited to database')return 
redirect(url_for('view'))else:#If the form does not have all fields that 
are required 
            flash('All fields are required.')return 
render_template('info.html', form=form)@app.route('/view', 
methods=['GET','POST'])def info():
  viewinfo =Contacts.query.all()return render_template('view.html', view =
viewinfo)@app.route('/edit<id>', methods=['GET','POST'])def edit(id):# How
can I edit the forms without Flask-Admin??????
|