#概要
Flaskを用いて、web上でのクイアプリ(ランダムに出題され、正誤判定が出る)を作成しています。
ローカルでは問題なく動くものの、herokuにデプロイした環境では特定のページが「Internal Server Error」 になってしまっています。
解決策をご教示いただけると嬉しいです。よろしくお願いいたします。
#ファイル構成
app.py
templates
|-base.html
|-index.html
|-question.html
quiz.csv
など。
トップページである「https://shikaq.herokuapp.com/」は正常に表示されますが、「https://shikaq.herokuapp.com/question」を開くとエラーが出てしまう状況です。
#herokuのログ
herokulog
12021-12-18T04:11:09.679243+00:00 app[web.1]: [2021-12-18 04:11:09,678] ERROR in app: Exception on /question [GET] 22021-12-18T04:11:09.679252+00:00 app[web.1]: Traceback (most recent call last): 32021-12-18T04:11:09.679253+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-packages/flask/app.py", line 2070, in wsgi_app 42021-12-18T04:11:09.679253+00:00 app[web.1]: response = self.full_dispatch_request() 52021-12-18T04:11:09.679254+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-packages/flask/app.py", line 1515, in full_dispatch_request 62021-12-18T04:11:09.679255+00:00 app[web.1]: rv = self.handle_user_exception(e) 72021-12-18T04:11:09.679255+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-packages/flask/app.py", line 1513, in full_dispatch_request 82021-12-18T04:11:09.679256+00:00 app[web.1]: rv = self.dispatch_request() 92021-12-18T04:11:09.679256+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-packages/flask/app.py", line 1499, in dispatch_request 102021-12-18T04:11:09.679256+00:00 app[web.1]: return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args) 112021-12-18T04:11:09.679257+00:00 app[web.1]: File "/app/app.py", line 79, in read 122021-12-18T04:11:09.679257+00:00 app[web.1]: + str(q_num)/10*100 + '%;" aria-valuenow="' + str(q_num)/10*100 132021-12-18T04:11:09.679257+00:00 app[web.1]: NameError: name 'q_num' is not defined 142021-12-18T04:11:09.679952+00:00 app[web.1]:xxx - - [18/Dec/2021:04:11:09 +0000] "GET /question HTTP/1.1" 500 290 "https://shikaq.herokuapp.com/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/xxx (KHTML, like Gecko) Chrome/xxx 15 Safari/xxx" 162021-12-18T04:11:09.682762+00:00 heroku[router]: at=info method=GET path="/question" host=shikaq.herokuapp.com request_id=XXX fwd="xxx" dyno=web.1 connect=0ms service=2ms status=500 bytes=463 protocol=https
#該当のソースコード
python
1from flask import Flask, render_template, request 2import reload 3import sys 4sys.dont_write_bytecode = True 5 6app = Flask(__name__) 7 8global df_all 9df_all = reload.df 10 11 12@app.route('/') 13def index(): 14 15 global df_all 16 df_all = reload.df 17 18 global q_df 19 q_df = reload.df 20 q_df = q_df.sample(n=10).reset_index(drop=True) 21 22 global list_question 23 list_question = list(q_df.loc[:, 'question']) 24 25 global list_q_type 26 list_q_type = list(q_df.loc[:, 'q_type']) 27 28 global list_choice_1 29 list_choice_1 = list(q_df.loc[:, 'choice_1']) 30 31 global list_choice_2 32 list_choice_2 = list(q_df.loc[:, 'choice_2']) 33 34 global list_choice_3 35 list_choice_3 = list(q_df.loc[:, 'choice_3']) 36 37 global list_choice_4 38 list_choice_4 = list(q_df.loc[:, 'choice_4']) 39 40 global list_choice_5 41 list_choice_5 = list(q_df.loc[:, 'choice_5']) 42 43 global list_answer 44 list_answer = list(q_df.loc[:, 'answer']) 45 46 global list_category 47 list_category = list(q_df.loc[:, 'category']) 48 49 global list_comment 50 list_comment = list(q_df.loc[:, 'comment']) 51 52 global list_index 53 list_index = list(q_df.index) 54 55 global list_id 56 list_id = list(q_df.loc[:, 'id']) 57 58 global list_choice 59 list_choice = [] 60 61 global list_truefalse 62 list_truefalse = [] 63 64 global q_num # リスト指定用。その問題が表示された時にリストの何番目を選択するのか 65 q_num = 0 66 67 return render_template('index.html') 68 69 70@app.route('/question', methods=['GET', 'POST']) 71def read(): 72 global q_num 73 if request.method == 'GET': 74 choice = request.args.get('send', 'None Selected') 75 76 progress = '<div class="progress-bar" role="progressbar" style="width:' 77 + str(q_num)/10*100 + '%;" aria-valuenow="' + str(q_num)/10*100 78 + '" aria-valuemin="0" aria-valuemax="100"></div>' 79 return render_template('question.html', choice=choice, 80 list_question=list_question, 81 list_q_type=list_q_type, 82 list_choice_1=list_choice_1, 83 list_choice_2=list_choice_2, 84 list_choice_3=list_choice_3, 85 list_choice_4=list_choice_4, 86 list_choice_5=list_choice_5, 87 list_answer=list_answer, 88 list_category=list_category, 89 list_comment=list_comment, 90 list_index=list_index, 91 q_num=q_num, 92 list_id=list_id, 93 list_truefalse=list_truefalse, 94 progress=progress) 95 96 elif request.method == 'POST': 97 choice = request.form.get("send") 98 list_choice.append(choice) 99 progress = '<div class="progress-bar" role="progressbar" style="width:' 100 + str(q_num)/10*100 + '%;" aria-valuenow="' + str(q_num)/10*100 101 + '" aria-valuemin="0" aria-valuemax="100"></div>' 102 list_truefalse.append(list_choice[q_num] == list_answer[q_num]) 103 q_num = len(list_choice) 104 105 return render_template('question.html', choice=choice, 106 list_question=list_question, 107 list_q_type=list_q_type, 108 list_choice_1=list_choice_1, 109 list_choice_2=list_choice_2, 110 list_choice_3=list_choice_3, 111 list_choice_4=list_choice_4, 112 list_choice_5=list_choice_5, 113 list_answer=list_answer, 114 list_category=list_category, 115 list_comment=list_comment, 116 list_index=list_index, 117 list_choice=list_choice, 118 q_num=q_num, 119 list_id=list_id, 120 list_truefalse=list_truefalse, 121 progress=progress) 122 123 124@app.route('/result') 125def result(): 126 true_count = list_truefalse.count(True) 127 answered_count = len(list_truefalse) 128 true_rate = true_count / answered_count 129 130 pie = "<div class='pie' style='display: flex; justify-content: center;" 131 + "align-items: center; margin-right: auto; margin-left: auto;" 132 +"width: 300px; height: 300px; font-size: 26px; font-weight: 700;" 133 + "background-image: radial-gradient(#f2f2f2 30%, transparent 31%)," 134 + "conic-gradient(#0A8EA0 0% " 135 + str(round(true_rate*100)) + "%, #d9d9d9 " + str(round(true_rate*100)) 136 + "% 100%); border-radius: 50%;'>" 137 + str(round(true_rate*100)) + "%</div>" 138 139 return render_template('result.html', 140 list_question=list_question, 141 list_q_type=list_q_type, 142 list_choice_1=list_choice_1, 143 list_choice_2=list_choice_2, 144 list_choice_3=list_choice_3, 145 list_choice_4=list_choice_4, 146 list_choice_5=list_choice_5, 147 list_answer=list_answer, 148 list_comment=list_comment, 149 list_index=list_index, 150 q_num=q_num, 151 list_id=list_id, 152 list_truefalse=list_truefalse, 153 true_rate=true_rate, 154 pie=pie) 155 156 157@app.route('/qanda/<int:id>') 158def read_qa(id): 159 post = df_all.loc[id] 160 161 return render_template('qanda.html', post=post) 162 163 164@app.errorhandler(404) 165def page_not_found(error): 166 return render_template('404.html'), 404 167 168 169if __name__ == "__main__": 170 app.run() 171
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/12/19 09:25