前提・実現したいこと
ウェブページにボタンを作り、ボタンを押すとほかのページに行くような処理をJavaScriptで書き、ページの移動が確認できています。
しかしflaskを使うとページの移動がうまくできずにNOT FOUNDというページになってしまいます。
発生している問題・エラーメッセージ
Not Found The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.
該当のソースコード
javascript
1 2function performLogIn() { 3 var name_us = $('input[name = "name_us"]').val(); 4 var emauil_us = $('input[name = "emauil_us"]').val(); 5 var pass_us = $('input[name = "pass_us"]').val(); 6 var conf_pass_us = $('input[name = "conf_pass_us"]').val(); 7 8 //var username = document.getElementsByTagName("username").value; 9 console.log(name_us); 10 console.log(emauil_us); 11 location.href="../index.html"; 12} 13 14
省略していますが。
html
1<!doctype html> 2 3<html lang="ja"> 4 <head> 5 <meta charset = "UTF-8"> 6 7 <link rel="stylesheet" href="/static/css/style.css"> 8 9 <title>mypage</title> 10 11 </head> 12 13 14 </div> 15<div class="cont_btn"> 16 <!--下のボタン--> 17 <a id = "login-button" href = "javascript:performLogIn()" input type =button class="btn_sign">SIGN IN</button></a> 18 19 </div> 20 21 22 <script src="/static/js/login.js"></script> 23 <script src="/static/js/jquery-3.3.1.min.js"></script> </body> 24 </html>
python
1from flask import Flask, render_template 2app = Flask(__name__) 3 4@app.route('/') 5def hello(): 6 with open("index.html", "rb") as f: 7 return render_template('index.html') 8 9 10if __name__ == "__main__": 11 app.run(debug=True,port=8084) 12 print("http://localhost:" + str(port))
ボタンを押したらjavascriptのperformLogIn()という関数を呼び出しています。
その関数ではURLを指定できるためにlocation.href="../main.html";としています
ボタンをおすとURLがhttp://localhost:8084/main.htmlと変わっていることも確認していますが、NOTFOUNDとなります。
試したこと
javascriptの
location.href="../index.html";を
location.href="";とすると再度同じページが表示されました。そのときのURLはhttp://localhost:8084/でした。(http://localhost:8084にindex.htmlが置かれている?) そう考え、pythonのプログラムを下のように変えるとmain.htmlが読み込まれましたが、わざわざこのようにしないとflaskはページが読み込めないのでしょうか?
index.htmlに書くだけではダメなのか。
python
1from flask import Flask, render_template 2app = Flask(__name__) 3 4@app.route('/') 5def hello(): 6 with open("index.html", "rb") as f: 7 return render_template('index.html') 8 9@app.route('/main.html') 10def main(): 11 with open("main.html", "rb") as f: 12 return render_template('main.html') 13 14if __name__ == "__main__": 15 app.run(debug=True,port=8084) 16 print("http://localhost:" + str(port))
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/03/20 13:29
2019/03/23 02:16
2019/04/19 23:11