librelist archives

« back to archive

flask jinja looping

flask jinja looping

From:
peg kh
Date:
2014-12-12 @ 07:50
I have a wtform in contact.html and wanna display the data that user enters
into info.html
but nothing is showing up.I'm using Jinja for looping,  I think the problem
is in `{% for ?? in Contacts %}`also try {% for entry in form %}




info.html

    <h2>show the info</h2>
    # what should be in ?? to display entry of table Conatcts
    {% for ??? in ??? %}
        # I guess it should be
        #{% for contactinfo in Contacts %}
        #but it would show nothing
        name: {{ ??.name}} <br>
        email: {{ ??.email}} <br>
    {% endfor %}

<!-- -->

    # route for contact
    # which show the field that should fill out by user
    @app.route('/contact', methods=['GET', 'POST'])
    def contact():
        form = LoginForm()
            if request.method == 'POST':
                if form.validate()== True:
                    newcontact = Contacts(name=form.name,email=form.name )
                    db.session.add(newcontact)
                    # connect to database
                    db.session.commit()
                    return redirect(url_for('info'))

    #info route
    @app.route('/info', methods=['GET', 'POST'])
    def info():
        form = LoginForm()
        #fetch the first entry
        contactinfo = Contacts.query.first()
        # populate the form
        # to store the entry
        form.name = contactinfo.name.data
        form.email = contactinfo.email.data
        #returns the html page, along with the form
        return render_template('info.html', form=form)

    if __name__ == '__main__':
        app.run(debug=True)
<!-- -->
form.py

    # the form that define each field
    # the form that will show up in contact route
    class LoginForm(Form):
        name = StringField("Name", [validators.Required()])
        email = StringField("Email", [validators.Required()])

model.py

    #table that store entry that user enter in wtform
    #it has two column name and email
    class Contacts(db.Model):
    __tablename__ = "Contacts"
    id = db.Column(db.Integer, primary_key = True)
    name = db.Column(db.String(50))

Re: [flask] flask jinja looping

From:
Raimon Esteve
Date:
2014-12-12 @ 12:16
2014-12-12 8:50 GMT+01:00 peg kh <peg1988.kh@gmail.com>:
> I have a wtform in contact.html and wanna display the data that user enters
> into info.html
> but nothing is showing up.I'm using Jinja for looping,  I think the problem
> is in `{% for ?? in Contacts %}`also try {% for entry in form %}
>
>
>
>
> info.html
>
>     <h2>show the info</h2>
>     # what should be in ?? to display entry of table Conatcts
>     {% for ??? in ??? %}
>         # I guess it should be
>         #{% for contactinfo in Contacts %}
>         #but it would show nothing
>         name: {{ ??.name}} <br>
>         email: {{ ??.email}} <br>
>     {% endfor %}

In python is:

    if form.validate_on_submit():
        data = {}
        for k, v in request.form.iteritems():
            print k, v

In jinja is:

      {% for key, value in request.form.iteritems() %}
            <dt>{{ key }}</dt>
            <dd>{{ value }}</dd>
      {% endfor %}