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

質問編集履歴

6

説明文追加

2021/05/06 00:10

投稿

yujin1202
yujin1202

スコア61

title CHANGED
File without changes
body CHANGED
@@ -1,3 +1,10 @@
1
+ <後記、2021年5月6日> 
2
+ 全体像として、何をやりたかったを、Githubに記載しました。
3
+ 宜しかったら、眺めてください。改めて、皆さま、ありがとうございました。
4
+ https://github.com/kazu0116/web_application
5
+
6
+ =====================
7
+
1
8
  HTMLと言うか、Jinja2の書き方の問題と言う気がするのですが、どうしても分かりません。
2
9
  教えてください。
3
10
 

5

誤字修正

2021/05/06 00:10

投稿

yujin1202
yujin1202

スコア61

title CHANGED
File without changes
body CHANGED
@@ -150,7 +150,7 @@
150
150
  print(list1s) #コンソール上で、キチンとデータが取得できているかどうかの確認用
151
151
 
152
152
 
153
- return render_template( 'index2.html', list1s = list1s)
153
+ return render_template( 'index1.html', list1s = list1s)
154
154
 
155
155
  if __name__ == '__main__':
156
156
  app.run()
@@ -200,19 +200,19 @@
200
200
  <head>
201
201
  <meta charset="utf-8">
202
202
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
203
- <title>test_list</title>
203
+ <title>Hello World</title>
204
204
  </head>
205
205
  <body>
206
- <h1>{{ title }}</h1>
206
+ <h1>{{ title }}</h1>
207
- {% block content %}
207
+ {% block content %}
208
208
  {% for list1 in list1s %}
209
- <p>{{list1}}</p>
209
+ <p>{{loop.index}}:{{list1}}</p>
210
- {% endfor %}
210
+ {% endfor %}
211
- {% endblock %} --!>
211
+ {% endblock %}
212
212
  </form>
213
213
  </body>
214
214
 
215
- </html>
215
+ </html>コード
216
216
  ```
217
217
  4.mysqlのtb3のテーブル内容
218
218
 

4

解決済の最終コードを追記(御参考)

2021/05/03 05:51

投稿

yujin1202
yujin1202

スコア61

title CHANGED
File without changes
body CHANGED
@@ -97,4 +97,132 @@
97
97
 
98
98
  複数項目を表示(HTML上に)させる為には、HTML(index1.html)をどの様に書けば良いのでしょうか?
99
99
  丸2日間、色々と試したのですが、どうしても上手くいきません。
100
- よろしくお願いいたします。
100
+ よろしくお願いいたします。
101
+
102
+ ************
103
+ 2021年5月3日、TakaiYさんのアドバイスに基づき、解決しております。
104
+ 最終コードは下記です。(御参考まで)
105
+
106
+ 1. pythonのコード
107
+ ```ここに言語を入力
108
+ from flask import Flask, render_template,request
109
+ import pymysql
110
+ app = Flask(__name__)
111
+
112
+ @app.route('/', methods=['GET'])
113
+ def get():
114
+ return render_template('index.html', \
115
+ title = 'Form Sample(get)', \
116
+ message = 'Where do you want to go?')
117
+
118
+
119
+ def getConnection():
120
+ return pymysql.connect(
121
+ host='localhost',
122
+ db='first_db',
123
+ user='root',
124
+ password='yireozna',
125
+ charset='utf8',
126
+ cursorclass=pymysql.cursors.DictCursor
127
+ )
128
+
129
+
130
+ @app.route('/', methods=['POST'])
131
+ def select_sql():
132
+ connection = getConnection()
133
+ message = "test"
134
+ names = request.form.getlist('checkbox')
135
+
136
+ list1s=[]
137
+ for name in names:
138
+
139
+ cursor = connection.cursor()
140
+ sql = "select Country, Agency, email from tb3 where Country=%s";
141
+ cursor.execute(sql, (name,))
142
+
143
+ list = cursor.fetchall()
144
+ list1s.append(list1)
145
+
146
+
147
+ cursor.close()
148
+ connection.close()
149
+
150
+ print(list1s) #コンソール上で、キチンとデータが取得できているかどうかの確認用
151
+
152
+
153
+ return render_template( 'index2.html', list1s = list1s)
154
+
155
+ if __name__ == '__main__':
156
+ app.run()
157
+ ```
158
+
159
+ 2.index.htmlのコード
160
+ ```ここに言語を入力
161
+ <!doctype html>
162
+ <html lang="ja">
163
+ <head>
164
+ <meta charset="utf-8">
165
+ <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
166
+ <title>Hello World</title>
167
+ </head>
168
+ <body>
169
+ <h1>{{ title }}</h1>
170
+ <p>{{ message }}</p>
171
+ <form action="/" method="POST" enctype="multipart/form-data">
172
+ <div>
173
+ <label for="ck1">Afgahanistan:</label>
174
+ <input type="checkbox" id="ck1" name="checkbox" value="Afgahanistan">
175
+ </div>
176
+ <div>
177
+ <label for="ck2">Australia:</label>
178
+ <input type="checkbox" id="ck2" name="checkbox" value="Australia">
179
+ </div>
180
+ <div>
181
+ <label for="ck3">England:</label>
182
+ <input type="checkbox" id="ck3" name="checkbox" value="England">
183
+ </div>
184
+ <div>
185
+ <label for="ck4">France:</label>
186
+ <input type="checkbox" id="ck4" name="checkbox" value="France">
187
+ </div>
188
+ <div>
189
+ <input type="submit" value="送信">
190
+ </div>
191
+ </form>
192
+ </body>
193
+ </html>
194
+
195
+
196
+ 3.index1.htmlのコード
197
+ ```ここに言語を入力
198
+ <!doctype html>
199
+ <html lang="ja">
200
+ <head>
201
+ <meta charset="utf-8">
202
+ <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
203
+ <title>test_list</title>
204
+ </head>
205
+ <body>
206
+ <h1>{{ title }}</h1>
207
+ {% block content %}
208
+ {% for list1 in list1s %}
209
+ <p>{{list1}}</p>
210
+ {% endfor %}
211
+ {% endblock %} --!>
212
+ </form>
213
+ </body>
214
+
215
+ </html>
216
+ ```
217
+ 4.mysqlのtb3のテーブル内容
218
+
219
+ mysql> select * from tb3;
220
+ +--------------+------------+------------------+
221
+ | Country | Agency | email |
222
+ +--------------+------------+------------------+
223
+ | Afgahanistan | ABC Travel | 123@gmail.com |
224
+ | Australia | DEF Travel | 456@yahoo.com |
225
+ | England | GHI Travel | 789@hotmail.com |
226
+ | France | JFK Travel | 1111@yahoo.co.jp |
227
+ +--------------+------------+------------------+
228
+ 4 rows in set (0.00 sec)

3

誤字修正

2021/05/03 05:49

投稿

yujin1202
yujin1202

スコア61

title CHANGED
File without changes
body CHANGED
@@ -55,7 +55,7 @@
55
55
  return render_template( 'index1.html', list1s = list1s) 
56
56
 
57
57
  if __name__ == '__main__':
58
- app.run()コード
58
+ app.run()
59
59
  ```
60
60
 
61
61
  コンソール上への表示は、1つの国を選択した場合も、複数の国を選択した場合も、期待した通りのデータを返してきます。(ここまでは、OK。下記は、3か国を選択した場合のコンソール表示。)

2

コードの抜けを修正(ケアレスミスです。)

2021/05/01 11:40

投稿

yujin1202
yujin1202

スコア61

title CHANGED
File without changes
body CHANGED
@@ -46,7 +46,7 @@
46
46
  sql = "select Country, Agency, email from tb3 where Country=%s";
47
47
  cursor.execute(sql, (name,))
48
48
 
49
- list1s = cursor.fetchall() #TakakiYさんのコメントを受け、誤字修正
49
+ list1s = cursor.fetchall() #TakaiYさんのコメントを受け、誤字修正
50
50
  print(list1) #コンソール上での動作確認用
51
51
 
52
52
  cursor.close()  

1

コードの抜けを修正(ケアレスミス修正)

2021/05/01 11:23

投稿

yujin1202
yujin1202

スコア61

title CHANGED
File without changes
body CHANGED
@@ -46,8 +46,8 @@
46
46
  sql = "select Country, Agency, email from tb3 where Country=%s";
47
47
  cursor.execute(sql, (name,))
48
48
 
49
- list1 = cursor.fetchall()
49
+ list1s = cursor.fetchall() #TakakiYさんのコメントを受け、誤字修正
50
- print(list1) #コンソール上での動作確認用
50
+ print(list1) #コンソール上での動作確認用
51
51
 
52
52
  cursor.close()  
53
53
  connection.close()