質問編集履歴

6

説明文追加

2021/05/06 00:10

投稿

yujin1202
yujin1202

スコア56

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

5

誤字修正

2021/05/06 00:10

投稿

yujin1202
yujin1202

スコア56

test CHANGED
File without changes
test CHANGED
@@ -302,7 +302,7 @@
302
302
 
303
303
 
304
304
 
305
- return render_template( 'index2.html', list1s = list1s)
305
+ return render_template( 'index1.html', list1s = list1s)
306
306
 
307
307
 
308
308
 
@@ -402,23 +402,23 @@
402
402
 
403
403
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
404
404
 
405
- <title>test_list</title>
405
+ <title>Hello World</title>
406
406
 
407
407
  </head>
408
408
 
409
409
  <body>
410
410
 
411
- <h1>{{ title }}</h1>
411
+ <h1>{{ title }}</h1>
412
-
412
+
413
- {% block content %}
413
+ {% block content %}
414
414
 
415
415
  {% for list1 in list1s %}
416
416
 
417
- <p>{{list1}}</p>
417
+ <p>{{loop.index}}:{{list1}}</p>
418
-
418
+
419
- {% endfor %}
419
+ {% endfor %}
420
-
420
+
421
- {% endblock %} --!>
421
+ {% endblock %}
422
422
 
423
423
  </form>
424
424
 
@@ -426,7 +426,7 @@
426
426
 
427
427
 
428
428
 
429
- </html>
429
+ </html>コード
430
430
 
431
431
  ```
432
432
 

4

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

2021/05/03 05:51

投稿

yujin1202
yujin1202

スコア56

test CHANGED
File without changes
test CHANGED
@@ -197,3 +197,259 @@
197
197
  丸2日間、色々と試したのですが、どうしても上手くいきません。
198
198
 
199
199
  よろしくお願いいたします。
200
+
201
+
202
+
203
+ ************
204
+
205
+ 2021年5月3日、TakaiYさんのアドバイスに基づき、解決しております。
206
+
207
+ 最終コードは下記です。(御参考まで)
208
+
209
+
210
+
211
+ 1. pythonのコード
212
+
213
+ ```ここに言語を入力
214
+
215
+ from flask import Flask, render_template,request
216
+
217
+ import pymysql
218
+
219
+ app = Flask(__name__)
220
+
221
+
222
+
223
+ @app.route('/', methods=['GET'])
224
+
225
+ def get():
226
+
227
+ return render_template('index.html', \
228
+
229
+ title = 'Form Sample(get)', \
230
+
231
+ message = 'Where do you want to go?')
232
+
233
+
234
+
235
+
236
+
237
+ def getConnection():
238
+
239
+ return pymysql.connect(
240
+
241
+ host='localhost',
242
+
243
+ db='first_db',
244
+
245
+ user='root',
246
+
247
+ password='yireozna',
248
+
249
+ charset='utf8',
250
+
251
+ cursorclass=pymysql.cursors.DictCursor
252
+
253
+ )
254
+
255
+
256
+
257
+
258
+
259
+ @app.route('/', methods=['POST'])
260
+
261
+ def select_sql():
262
+
263
+ connection = getConnection()
264
+
265
+ message = "test"
266
+
267
+ names = request.form.getlist('checkbox')
268
+
269
+
270
+
271
+ list1s=[]
272
+
273
+ for name in names:
274
+
275
+
276
+
277
+ cursor = connection.cursor()
278
+
279
+ sql = "select Country, Agency, email from tb3 where Country=%s";
280
+
281
+ cursor.execute(sql, (name,))
282
+
283
+
284
+
285
+ list = cursor.fetchall()
286
+
287
+ list1s.append(list1)
288
+
289
+
290
+
291
+
292
+
293
+ cursor.close()
294
+
295
+ connection.close()
296
+
297
+
298
+
299
+ print(list1s) #コンソール上で、キチンとデータが取得できているかどうかの確認用
300
+
301
+
302
+
303
+
304
+
305
+ return render_template( 'index2.html', list1s = list1s)
306
+
307
+
308
+
309
+ if __name__ == '__main__':
310
+
311
+ app.run()
312
+
313
+ ```
314
+
315
+
316
+
317
+ 2.index.htmlのコード
318
+
319
+ ```ここに言語を入力
320
+
321
+ <!doctype html>
322
+
323
+ <html lang="ja">
324
+
325
+ <head>
326
+
327
+ <meta charset="utf-8">
328
+
329
+ <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
330
+
331
+ <title>Hello World</title>
332
+
333
+ </head>
334
+
335
+ <body>
336
+
337
+ <h1>{{ title }}</h1>
338
+
339
+ <p>{{ message }}</p>
340
+
341
+ <form action="/" method="POST" enctype="multipart/form-data">
342
+
343
+ <div>
344
+
345
+ <label for="ck1">Afgahanistan:</label>
346
+
347
+ <input type="checkbox" id="ck1" name="checkbox" value="Afgahanistan">
348
+
349
+ </div>
350
+
351
+ <div>
352
+
353
+ <label for="ck2">Australia:</label>
354
+
355
+ <input type="checkbox" id="ck2" name="checkbox" value="Australia">
356
+
357
+ </div>
358
+
359
+ <div>
360
+
361
+ <label for="ck3">England:</label>
362
+
363
+ <input type="checkbox" id="ck3" name="checkbox" value="England">
364
+
365
+ </div>
366
+
367
+ <div>
368
+
369
+ <label for="ck4">France:</label>
370
+
371
+ <input type="checkbox" id="ck4" name="checkbox" value="France">
372
+
373
+ </div>
374
+
375
+ <div>
376
+
377
+ <input type="submit" value="送信">
378
+
379
+ </div>
380
+
381
+ </form>
382
+
383
+ </body>
384
+
385
+ </html>
386
+
387
+
388
+
389
+
390
+
391
+ 3.index1.htmlのコード
392
+
393
+ ```ここに言語を入力
394
+
395
+ <!doctype html>
396
+
397
+ <html lang="ja">
398
+
399
+ <head>
400
+
401
+ <meta charset="utf-8">
402
+
403
+ <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
404
+
405
+ <title>test_list</title>
406
+
407
+ </head>
408
+
409
+ <body>
410
+
411
+ <h1>{{ title }}</h1>
412
+
413
+ {% block content %}
414
+
415
+ {% for list1 in list1s %}
416
+
417
+ <p>{{list1}}</p>
418
+
419
+ {% endfor %}
420
+
421
+ {% endblock %} --!>
422
+
423
+ </form>
424
+
425
+ </body>
426
+
427
+
428
+
429
+ </html>
430
+
431
+ ```
432
+
433
+ 4.mysqlのtb3のテーブル内容
434
+
435
+
436
+
437
+ mysql> select * from tb3;
438
+
439
+ +--------------+------------+------------------+
440
+
441
+ | Country | Agency | email |
442
+
443
+ +--------------+------------+------------------+
444
+
445
+ | Afgahanistan | ABC Travel | 123@gmail.com |
446
+
447
+ | Australia | DEF Travel | 456@yahoo.com |
448
+
449
+ | England | GHI Travel | 789@hotmail.com |
450
+
451
+ | France | JFK Travel | 1111@yahoo.co.jp |
452
+
453
+ +--------------+------------+------------------+
454
+
455
+ 4 rows in set (0.00 sec)

3

誤字修正

2021/05/03 05:49

投稿

yujin1202
yujin1202

スコア56

test CHANGED
File without changes
test CHANGED
@@ -112,7 +112,7 @@
112
112
 
113
113
  if __name__ == '__main__':
114
114
 
115
- app.run()コード
115
+ app.run()
116
116
 
117
117
  ```
118
118
 

2

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

2021/05/01 11:40

投稿

yujin1202
yujin1202

スコア56

test CHANGED
File without changes
test CHANGED
@@ -94,7 +94,7 @@
94
94
 
95
95
 
96
96
 
97
- list1s = cursor.fetchall() #TakakiYさんのコメントを受け、誤字修正
97
+ list1s = cursor.fetchall() #TakaiYさんのコメントを受け、誤字修正
98
98
 
99
99
  print(list1) #コンソール上での動作確認用
100
100
 

1

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

2021/05/01 11:23

投稿

yujin1202
yujin1202

スコア56

test CHANGED
File without changes
test CHANGED
@@ -94,9 +94,9 @@
94
94
 
95
95
 
96
96
 
97
- list1 = cursor.fetchall()
97
+ list1s = cursor.fetchall() #TakakiYさんのコメントを受け、誤字修正
98
98
 
99
- print(list1) #コンソール上での動作確認用
99
+ print(list1) #コンソール上での動作確認用
100
100
 
101
101
 
102
102