如何使用Flask创建显示对象属性的表 - python

我正在尝试进行搜索,以向用户显示SQLAlchemy数据库查询的结果。但是,似乎返回的是一个空列表-该表具有标题,但没有其他行。我已检查以确保获得结果,所以我不知道出了什么问题。我尝试过以原始形式和字典列表发送数据,但是仍然没有运气。有什么见解吗?

for result in results:
    resultData = {'system_name':result.system_name,'system_description':result.system_description,'system_tags':[tag.tag_name for tag in result.tags],'system_date_created':result.system_date_created,'system_hits':result.system_hits}
    response.append(resultData)
print response
return render_template('index.html', searchResults=response)

index.html

<table class='table table-hover'>
<tr>
<th>Name</th>
<th>Description</th>
<th>Tags</th>
<th>Date Created</th>
<th>Views</th>
</tr>
{% for result in searchResults %}
    <tr>
    <td>{{ result.system_name }}</td>
    <td>{{ result.system_description }}</td>
    <td>Tags</td>
    <td>{{ result.system_date_created }}</td>
    <td>{{ result.system_hits }}</td>
    </tr>
{% endfor %}
</table>

参考方案

这是一个通过一些嵌套数据提供html表的快速示例。如建议的那样,还有一个带有jsonify数据的额外端点用于调试。

app.py

from flask import Flask, render_template, jsonify
import namedtupled as nt  # for debugging 

app = Flask(__name__)

# set up some fake data
foo = { 'system_name': 'foo', 'system_description': 'bar',
        'tags': [{'tag_name': 'happy'}, {'tag_name': 'cat'}],
        'system_date_created': 'today', 'system_hits': 100 }
fuu = { 'system_name': 'fuu', 'system_description': 'bor',
        'tags': [{'tag_name': 'cute'}, {'tag_name': 'puppy'}],
        'system_date_created': 'yesterday', 'system_hits': 99 }

# make fake data classy so it fits your example
results = [nt.map(foo), nt.map(fuu)]  

def refine_results(results):
    """ Refine some search results. """
    # something here, maybe a request for search results
    data = []
    for result in results:
        resultData = {'system_name': result.system_name,
                      'system_description': result.system_description,
                      'system_tags': [tag.tag_name for tag in result.tags],
                      'system_date_created': result.system_date_created,
                      'system_hits': result.system_hits}
        data.append(resultData)
    return data

@app.route('/json')
def data():
    """ Extra endpoint for debugging expected data. """
    searchResults = refine_results(results)
    return jsonify(searchResults)

@app.route('/table')
def table():
    """ html table with data """
    searchResults = refine_results(results)
    return render_template('index.html', searchResults=searchResults)

if __name__ == '__main__':
    app.run(debug=True)

index.html

<table>
  <tr>
    <th>Name</th>
    <th>Description</th>
    <th>Tags</th>
    <th>Date Created</th>
    <th>Views</th>
  </tr>
  {% for result in searchResults %}
      <tr>
        <td>{{ result.system_name }}</td>
        <td>{{ result.system_description }}</td>
        <td>
          {% for tag in result.system_tags %}
            {{tag}}
          {% endfor %}
        </td>
        <td>{{ result.system_date_created }}</td>
        <td>{{ result.system_hits }}</td>
      </tr>
  {% endfor %}
</table>

Python pytz时区函数返回的时区为9分钟 - python

由于某些原因,我无法从以下代码中找出原因:>>> from pytz import timezone >>> timezone('America/Chicago') 我得到:<DstTzInfo 'America/Chicago' LMT-1 day, 18:09:00 STD…

Python lmfit约束:a <b <c - python

我在Python中使用lmfit来拟合一些数据,其中包括拟合变量a,b和c。我需要确保a <b <c。我发现http://cars9.uchicago.edu/software/python/lmfit_MinimizerResult/constraints.html谈到需要定义为不等式和设置虚拟变量的约束。例如,如果我想要a + b <=…

PicklingError:无法使用python进程池执行程序对<type'function'>进行酸洗 - python

实用程序def exec_multiprocessing(self, method, args): with concurrent.futures.ProcessPoolExecutor() as executor: results = pool.map(method, args) return results clone.pydef clone_vm(se…

Python numpy数据指针地址无需更改即可更改 - python

编辑经过一些摆弄之后,到目前为止,我已经隔离了以下状态:一维数组在直接输入变量时提供两个不同的地址,而在使用print()时仅提供一个地址2D数组(或矩阵)在直接输入变量时提供三个不同的地址,在使用print()时提供两个地址3D数组在直接输入变量时提供两个不同的地址,而在使用print()时仅给出一个(显然与一维数组相同)像这样:>>> …

创建XML内联xmlns - python

我正在使用lxml创建XML输出的Python中工作,最终项目将需要将xml导入Strava,因此在导入之前需要正确的模式。下面是我创建的模式,我的问题是,如果我包含如下所示的XMLNS文本,我似乎无法正确设置格式。<Extensions> <TPX xmlns="http://www.garmin.com/xmlschemas/…