実現したいこと
HTMLの内容をPythonに引き渡す。
前提
HTMLで作成したテキストの内容をPythonに引き渡して表示する機能を作成しています。
発生している問題・エラーメッセージ
HTML上のフォームは表示されるがテキストを入力してsubmitすると
AttributeError: 'Request' object has no attribute 'index'
が表示される。
該当のソースコード
index.html
1<!DOCTYPE html> 2<html lang="ja"> 3 <head> 4 <meta charset="utf-8"> 5 <title>Hello</title> 6 </head> 7 <body> 8 <h1>Hello World{{ message }}</h1> 9 <form method="POST"> 10 <p>USERID<br> 11 <input type="text" name="userid"></p> 12 <p>PASSWORD<br> 13 <input type="text" name="passwd"></p> 14 <p><input type="submit" value="LOGIN"></p> 15 </form> 16 </body> 17</html> 18 19```app.py 20from flask import Flask, request, render_template 21 22# Flaskをインスタンス化している 23app = Flask(__name__) 24 25 26# デフォルトページの設定 27@app.route('/', methods=['GET', 'POST']) 28def index(): 29 # 2回目以降データが送られてきた時の処理です 30 if request.method == 'POST': 31 print(str(request.index['userid'])) 32 print(str(request.index['passwd'])) 33 return render_template('index.html') 34 # 1回目のデータが何も送られてこなかった時の処理です。 35 else: 36 return render_template('index.html') 37 38if __name__ == '__main__': 39 app.run(port=8000, debug=True) 40 41### 試したこと 42AttributeError を検索していくつかのサイトを確認しましたが対応が分かりませんでした。 43 44### 補足情報(FW/ツールのバージョンなど) 45Python 3.11
回答1件