■前提
以下のシステム構成でWebアプリケーションを作成していまして、ページ遷移のルーティング用コードを記述しましたが
post実行時に405エラーがでましたのでご存知の方でご教示頂けますと幸いです。
(エラーが出たのでGETに変更した際は400エラーBad Requestになり、どこを変更すれば良いか分からなく質問させて頂きました。)
■実現したいこと
以下URLを表示させたい。
example.com/login
(index.htmlは表示できていますが、上記URLを叩くとlogin()関数内でPOST/GETを確認しているが、上記のエラーが出ます)
■システム構成
OS:CentOS 7(VirtualBoxでのゲストOS)
言語:Python3.6.1
WEBサーバー:Nginx 1.12.1
WSGIサーバー:uWSGI 2.0.15
Webフレームワーク:Flask 0.12.2
テンプレートエンジン:Jinja2
###発生している問題・エラーメッセージ
■POST時 405 Method Not Allowed The method is not allowed for the requested URL. 以下、アクセスログ [pid: 5397|app: 0|req: 1/1] 192.168.**.* () {44 vars in 771 bytes} [Thu Sep 7 **:**:** 2017] GET /login => generated 178 bytes in 6 msecs (HTTP/1.1 405) 3 headers in 103 bytes (1 switches on core 0) --------- ■GET時 400 Bad Request The browser (or proxy) sent a request that this server could not understand. 以下、アクセスログ [pid: 5227|app: 0|req: 1/1] 192.168.**.* () {44 vars in 771 bytes} [Thu Sep 7 **:**:** 2017] GET /login => generated 192 bytes in 7 msecs (HTTP/1.1 400) 2 headers in 74 bytes (1 switches on core 0)
###該当のソースコード
Python
1 2#/var/www/testapp/app.py 3import os 4from flask import Flask,render_template,request,redirect,url_for,send_from_directory,session 5from werkzeug import secure_filename 6 7app = Flask(__name__) 8 9app.config['SECRET_KEY'] = os.urandom(24) 10 11@app.route('/') 12def index(): 13 if 'username' in session: 14 return render_template('index.html') 15 else: 16 return "<p> ログインしてください </p>" 17 18#GETの時はmethodsをGETに変更 19@app.route('/login',methods=['POST']) 20def login(): 21 if request.method == 'POST': 22 username = request.form['username'] 23 24 if username == 'admin': 25 session['username'] = request.form['username'] 26 return redirect(url_for('index')) 27 else: 28 return render_template('login.html',message="No user") 29 30@app.route('/logout') 31def logout(): 32 session.pop('username',None) 33 return redirect(url_for('index')) 34
html
1<!-- /var/www/testapp/templates/index.html --> 2{% extends "base.html" %} 3 4{% block content %} 5<p>indexページ</p> 6{% endblock %}
html
1<!-- /var/www/testapp/templates/base.html --> 2<!DOCTYPE html> 3<html lang="ja"> 4<head> 5 <meta charset="UTF-8"> 6 <link rel="stylesheet" href="/static/css/skeleton.css"> 7 <link rel="stylesheet" href="/static/css/style.css"> 8 <title>アップロード</title> 9</head> 10<body> 11<div class="container"> 12 <div class="row"> 13 {% block content %} 14 {% endblock %} 15 </div> 16</div> 17 18</body> 19</html>
html
1<!-- /var/www/testapp/templates/login.html --> 2{% extends "base.html" %} 3 4{% block content %} 5{{ message }} 6<form action="#" method="GET"> 7 <p><input type="text" name="username"></p> 8 <p><input type="submit" value="Login"></p> 9</form> 10{% endblock %} 11
###試したこと
過去の事例を調べましたら、Nginxで静的ファイルをPOSTで取れないとの記事を見つけました。
http://d.hatena.ne.jp/pullphone/20121116/1353054974
上記のURL参考にNginxのコンフィグファイル(default.conf)に以下を追記
error_page 405 = $uri;
nginx
1server { 2 listen 80; 3 server_name example.com; 4 location / { 5 include uwsgi_params; 6 uwsgi_pass unix:///tmp/uwsgi.sock; 7 error_page 405 = $uri; 8 } 9}
以下はuWSGIのiniファイルです。(こちらのファイルは変更はしていません)
nginx
1[uwsgi] 2module = app 3callable = app 4master = true 5processes = 1 6socket = /tmp/uwsgi.sock 7chmod-socket = 666 8vacuum = true 9die-on-term = true
■結果エラー等に変化ありませんでした。
###補足情報
あなたの回答
tips
プレビュー