Python3(Flask)でメールアドレスやパスワードを管理するシステムを作っています。
情報を登録する機能と登録した情報を閲覧する機能を実装中に以下のエラーメッセージが2つ発生しました。
発生している問題・エラーメッセージ
'''
エラーメッセージ
情報を登録する機能を実装時発生するエラー(reg_con.html)
エラーコード 400
Bad Request
The browser (or proxy) sent a request that this server could not understand.
情報を閲覧する機能を実装時発生するエラー(show_con.html)
エラーコード 405
Method Not Allowed
The method is not allowed for the requested URL.
'''
該当のソースコード
python
1from __future__ import with_statement 2from contextlib import closing 3 4import sqlite3 5from flask import Flask, request, session, g, redirect, url_for, \ 6 abort, render_template, flash 7 8DATABASE = './mailkey_account.db' 9DEBUG = True 10SECRET_KEY = 'development key' 11USERNAME = 'hogehoge' 12PASSWORD = 'hoge' 13 14app = Flask(__name__) 15app.config.from_object(__name__) 16 17def connect_db(): 18 return sqlite3.connect(app.config['DATABASE']) 19 20def init_db(): 21 with closing(connect_db()) as db: 22 with app.open_resource('schema.sql') as f: 23 db.cursor().executescript(f.read().decode('utf-8')) 24 db.commit() 25 26@app.before_request 27def before_request(): 28 g.db = connect_db() 29 30@app.after_request 31def after_request(response): 32 g.db.close() 33 return response 34 35#トップページ 36@app.route('/') 37def top(): 38 return redirect(url_for('login')) 39 40#ログイン ログアウト 41@app.route('/login', methods=['GET', 'POST']) 42def login(): 43 error = None 44 if request.method == 'POST': 45 if request.form['username'] != app.config['USERNAME']: 46 error = 'Invalid username' 47 elif request.form['password'] != app.config['PASSWORD']: 48 error = 'Invalid password' 49 else: 50 session['logged_in'] = True 51 flash('ログインしました') 52 return redirect(url_for('entry')) 53 #render_template('entry.html') 54 return render_template('login.html', error=error) 55 56#コンテンツを閲覧する 57@app.route('/show_con') 58def show_con(): 59 cur = g.db.execute('select service, email, pass from contents') 60 contents = [dict(service=row[0], email=row[1], passwords=row[2]) for row in cur.fetchall()] 61 return render_template('show_con.html', contents=contents) 62 63#登録情報追加 64@app.route('/reg_con', methods=['POST']) 65def reg_con(): 66 if not session.get('logged_in'): 67 abort(401) 68 g.db.execute('insert into contents (service, email, passwords) values (?, ?, ?)', 69 [request.form['service'], request.form['email'],request.form['passwords']]) 70 g.db.commit() 71 flash('登録が完了しました') 72 return redirect(url_for('show_con')) 73 74#登録 OR 閲覧ページ 75@app.route('/entry') 76def entry(): 77 return render_template('entry.html') 78 79if __name__ == '__main__': 80 app.run()
試したこと
エラーコードを検索し、類似したエラーを探したのですが、当方Python(Flask)の初心者なものでわかりませんでした。
補足情報
teratailのような質問サイトを初めて利用するため、情報に不備、蛇足等ありましたら申し訳ございません。また、教えていただければ、助かります。
プログラミングの経験が少ないため、掲載している情報に不足があるかもしれないので、その時はその都度修正致します。