【问题标题】:Return multiple lines in web.py在 web.py 中返回多行
【发布时间】:2014-11-07 22:51:07
【问题描述】:

我使用 MySQLdb 从 MySQL 中获取数据,但是当我尝试使用带有 JSON 的 web.py 在网页中显示它们时

fetch_resu = cur.fetchall()
json_list = []
for each_tup in fetch_resu:
    json_list.append(each_tup)
return json.dumps(json_list, encoding="UTF-8", ensure_ascii=False, indent=4, separators=(',', ': '))

我会得到一堆数据卡在网页的一行中(同一句话在本地调试中完美运行)

既然我只能返回数据,我怎样才能让它们更漂亮呢? 还是我应该使用模板?但是我的数据库是动态变化的。

【问题讨论】:

    标签: python mysql json django web.py


    【解决方案1】:

    恕我直言,有两种选择。

    • <pre>..</pre><code>..</code>包围内容

    • 回复为 text/plain 内容类型:

      web.header('Content-Type', 'text/plain')
      return json.dumps(...)
      

    【讨论】:

    • text/plain 有效,看来我需要了解更多 HTML 才能进一步了解
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 2016-07-21
    • 2021-05-13
    • 1970-01-01
    相关资源
    最近更新 更多