teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

修正

2018/12/21 22:58

投稿

8524ba23
8524ba23

スコア38352

answer CHANGED
@@ -1,7 +1,11 @@
1
+ - 提示されたjsonデータの辞書部分、カンマが含まれていないようですので、カンマで区切る必要があります。
2
+ - `test_find()`の戻り値がJSON文字列であれば、`json.loads`でデータ化する必要があります。
1
- `return render_template('test.html',test=test_data[0])`ではないでしょうか?
3
+ - `return render_template('test.html',test=test_data[0])`ではないでしょうか?
4
+
2
5
  以下当方での検証コードと結果です。テンプレートは提示HTMLをそのまま利用しています。
3
6
  ```Python
4
7
  from flask import Flask, render_template
8
+ import json # 追加
5
9
  app = Flask(__name__)
6
10
 
7
11
  @app.route('/')
@@ -10,8 +14,11 @@
10
14
 
11
15
  @app.route("/test/",methods=['GET'])
12
16
  def test():
13
- test_data = [{"username":"test", "password":"test", "memo":"hogehoge"}] # = test_find()
17
+ test_data = '[{"username":"test", "password":"test", "memo":"hogehoge"}]' # = test_find()
18
+ print(test_data) # [{"username":"test", "password":"test", "memo":"hogehoge"}]
19
+ test_data = json.loads(test_data) # json文字列をデータに変換
14
20
  print(test_data) # [{'password': 'test', 'username': 'test', 'memo': 'hogehoge'}]
21
+
15
22
  return render_template('test.html',test=test_data[0])
16
23
 
17
24
  if __name__ == '__main__':