@sideseat7 (しーも)

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

Flask初心者です

解決したいこと

ページの更新がうまくできない。
Flakにて.pyで記載したfor文の処理が終わるたび.htmlに表示したい

発生している問題・エラー

クイズゲームを作っています。
一問目が終わっても二問目に表示が変わってくれません。
頭の中ではhtmlの『回答』ボタンを押すことでhtmlのページが変わることなく、
Flask側で記入したfor文が機能して新しい値(二問目)が
htmlの{{}}に入って表示されると踏んだのですが。。
{{一問目}} 『回答』ボタンクリックすると→  {{二問目}}
という具合に。

該当するソースコード

```python Flask app.py
from flask import Flask, render_template, request

app = Flask(name)

questions = [
{"q":"問題1", "a":"答えA-1"},
{"q":"問題2", "a":"答えA-2"},
{"q":"問題3", "a":"答えB-3"}
]

selections = [
{"A":"答えA-1", "B":"答えB-1"},
{"A":"答えA-2", "B":"答えB-2"},
{"A":"答えA-3", "B":"答えB-3"}
]

Dict={}

@app.route('/', methods=['GET'])
def playQiz():

# スコアーはindex関数の中に書かないと機能してくれない
score = 0

for i in range(len(questions)):


    Dict['question'] = questions[i]['q']

    Dict['selectionA'] = selections[i]['A']
    Dict['selectionB'] = selections[i]['B']


    # ここは肝かもしれない
    # request.form.getからrequest.args.getに変更したらうまくいったが、なぞ
    Dict['flg'] = request.args.get('ko')      

    if Dict['flg']:
        if Dict['flg'] == questions[i]['a']:
            score += 1
            Dict['result'] = "正解 ◯ :{0}/3".format(score)
        else:
            Dict['result'] = "不正解 × :{0}/3".format(score)

    return render_template('index.html', Dict=Dict )

if name == 'main':
app.run(debug=True)

これはHTML

```html  -index.html-
<!DOCTYPE html>
<html lang="ja">
<head>
    <meta charset="UTF-8">    
    <title>AJQ</title>
</head>
<body>

    <p>{{Dict['question']}}</p>                <!--問題文表示-->

    <form method="get" action="/">
        <div>
            <input type="radio" name="ko" value="{{Dict['selectionA']}}" id="r1">    <!--選択肢ボタン表示-->
            <label for="r1">{{Dict['selectionA']}}</label>                           <!--選択肢A文表示-->
        </div>
        <div>
            <input type="radio" name="ko" value="{{Dict['selectionB']}}" id="r2">    <!--選択肢ボタン表示-->
            <label for="r2">{{Dict['selectionB']}}</label>                           <!--選択肢B文表示-->
        </div>

            <input type="submit" value="回答">           <!--回答しますよボタン-->

    </form>

    <h2>{{Dict['result']}}</h2>
</body>
</html>

自分で試したこと

methodの問題かと睨んだのですがそんな感じもせず

0 likes

2Answer

This answer has been deleted for violation of our Terms of Service.

This answer has been deleted for violation of our Terms of Service.

Your answer might help someone💌