【问题标题】:I need help to print a dictionary of objects as a html table我需要帮助将对象字典打印为 html 表
【发布时间】:2017-09-05 22:45:11
【问题描述】:

我的烧瓶应用程序正在完美地使用 flask-wtf 表单收集数据。收藏后

@home.route('/add-new', methods=['POST', 'GET'])
def newlist():
    return redirect( url_for('auth.login'))

    form = ShoppingList()
    if form.validate_on_submit():
        #insert to list
        item_id = len( shopping_lists ) + 1        
        shopping_list = ShoppingCart(session["email"], form.title.data, form.price.data, 
                                        form.quantity.data, item_id, form.description.data)
        addToDic(session["email"], shopping_list)
        result = shopping_lists
        flash("List saved okay")
        return render_template('home/dashboard.html', title="Dashboard", result = result)

    #Render the dashboard template on the /dashboard route    
    return render_template('home/newlist.html',form=form, title="Add new")

我的问题是如何将结果 dic 打印到烧瓶模板中的表格中。这是模板代码

<h3>HOME</h3>
                  <p>{{ utils.flashed_messages() }}</p>                   
                    <table class="table table-bordered">
                        <thead>
                          <tr>
                            <th>Item name</th>
                            <th>price</th>
                            <th>quantity</th>
                            <th>description</th>
                          </tr>
                        </thead>
                        <tbody>
                          <tr>

                          </tr>                          
                        </tbody>
                      </table>  

非常感谢。请联系

【问题讨论】:

    标签: python-3.x dictionary flask wtforms


    【解决方案1】:

    你应该咨询jinja2documentation,因为它是一个写得很好的和全面的知识来源,你想做的是非常简单的任务,前面已经解释过了。

    这是一个工作示例:

    app.py

    from flask import Flask
    from flask import render_template
    
    app = Flask(__name__)
    
    class MyObj(object):
        def __init__(self, name, price, quantity):
            self.name = name
            self.price = price
            self.quantity = quantity
    
    @app.route('/')
    def index():
        my_objects = [
            MyObj('I am a super-cool object!', 1000, 1),
            MyObj('I am a cooler object!', 2000, 2),
            MyObj('I am the coolest object!', 3000, 3),
        ]
        return render_template('index.html', my_objects=my_objects)
    
    if __name__ == '__main__':
        app.run(debug=True)
    

    index.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
        <style>
        table, th, td {
            border: 1px solid black;
        }
        </style>
    </head>
    <body>
    <table>
        <thead style="border: 1px solid black;">
        <tr>
            <th>Item name</th>
            <th>price</th>
            <th>quantity</th>
        </tr>
        </thead>
        <tbody>
        {% for ob in my_objects %}
        <tr>
            <td>{{ ob.name }}</td>
            <td>{{ ob.price }}</td>
            <td>{{ ob.quantity }}</td>
        </tr>
        {% endfor %}
        </tbody>
    </table>
    </body>
    </html>
    

    结果网页:

    【讨论】:

      猜你喜欢
      • 2015-01-30
      • 2013-02-12
      • 1970-01-01
      • 2014-07-19
      • 2011-08-15
      • 1970-01-01
      • 1970-01-01
      • 2013-06-17
      • 2017-03-31
      相关资源
      最近更新 更多