如何在flask中显示类对象列表

时间:2014-06-02 21:22:42

标签: python flask

我有一个python列表,其中列表中的每个元素都是类的对象:

message = [
    <File(tar_file:appstream-glib-0.1.5.tar.xz, filename:/appstream-glib-0.1.5/config.guess, sha256:4a86808b907403ad6801c0661a4524dfa07c0b898a2cef6e7fa0cf82a09b9c53)>, 
    <File(tar_file:totem-3.12.1.tar.xz, filename:/totem-3.12.1/build-aux/config.guess, sha256:4a86808b907403ad6801c0661a4524dfa07c0b898a2cef6e7fa0cf82a09b9c53)>,
    <File(tar_file:gvfs-1.20.2.tar.xz, filename:/gvfs-1.20.2/config.guess, sha256:4a86808b907403ad6801c0661a4524dfa07c0b898a2cef6e7fa0cf82a09b9c53)>,
    <File(tar_file:gnome-software-3.12.2.tar.xz, filename:/gnome-software-3.12.2/config.guess, sha256:4a86808b907403ad6801c0661a4524dfa07c0b898a2cef6e7fa0cf82a09b9c53)>,
    <File(tar_file:gnome-packagekit-3.12.2.tar.xz, filename:/gnome-packagekit-3.12.2/config.guess, sha256:4a86808b907403ad6801c0661a4524dfa07c0b898a2cef6e7fa0cf82a09b9c53)>,
    <File(tar_file:gnome-color-manager-3.12.2.tar.xz, filename:/gnome-color-manager-3.12.2/config.guess, sha256:4a86808b907403ad6801c0661a4524dfa07c0b898a2cef6e7fa0cf82a09b9c53)>,
    <File(tar_file:gnome-chess-3.12.2.tar.xz, filename:/gnome-chess-3.12.2/config.guess, sha256:4a86808b907403ad6801c0661a4524dfa07c0b898a2cef6e7fa0cf82a09b9c53)>,
    <File(tar_file:gnome-power-manager-3.12.2.tar.xz, filename:/gnome-power-manager-3.12.2/config.guess, sha256:4a86808b907403ad6801c0661a4524dfa07c0b898a2cef6e7fa0cf82a09b9c53)>,
    <File(tar_file:evolution-mapi-3.12.2.tar.xz, filename:/evolution-mapi-3.12.2/config.guess, sha256:4a86808b907403ad6801c0661a4524dfa07c0b898a2cef6e7fa0cf82a09b9c53)>,
    <File(tar_file:cockpit-0.7.tar.bz2, filename:/cockpit-0.7/tools/config.guess, sha256:4a86808b907403ad6801c0661a4524dfa07c0b898a2cef6e7fa0cf82a09b9c53)>,
    <File(tar_file:xf86-video-freedreno-1.1.0.tar.bz2, filename:/xf86-video-freedreno-1.1.0/config.guess, sha256:4a86808b907403ad6801c0661a4524dfa07c0b898a2cef6e7fa0cf82a09b9c53)>, 
    <File(tar_file:elfutils-0.159.tar.bz2, filename:/elfutils-0.159/config/config.guess, sha256:4a86808b907403ad6801c0661a4524dfa07c0b898a2cef6e7fa0cf82a09b9c53)>,
    <File(tar_file:ibus-table-1.5.0.20140519.tar.gz, filename:/ibus-table-1.5.0.20140519/config.guess, sha256:4a86808b907403ad6801c0661a4524dfa07c0b898a2cef6e7fa0cf82a09b9c53)>, 
    <File(tar_file:fence-agents-4.0.9.tar.xz, filename:/fence-agents-4.0.9/config.guess, sha256:4a86808b907403ad6801c0661a4524dfa07c0b898a2cef6e7fa0cf82a09b9c53)>
]

列表中每个元素的类型是 - &gt;班summershum.model.File

我希望将列表作为响应返回烧瓶并在localhost上显示其内容,但它不会显示任何内容。

这就是我这样做的方式。

return flask.Response(
        response=message,
    )

这样做的正确方法是什么?

1 个答案:

答案 0 :(得分:0)

这是您通常将对象从视图返回到模板的方式:

在您的观看方法中:

@app.route('/files')
def display_files():
    '''code to retrieve list of file objects'''
    return render_template('files.html', all_files=all_files)

然后在您的模板文件中,您可以访问all_files

  {% for a_file in all_files %}
      {{ a_file.tar_file }}
  {% endfor %}

烧瓶模板的文档here