2016-04-04 8 views
0

私はFlask APIに慣れようとしています。POSTメソッドを使用する1つのフォームを持つ簡単なページを作成しています。メソッドが許可されていません。フラスコはPOSTの代わりにGETを受け取ります

代わりにFlask出力を見ると、/ configureビューを開くときにいつでもFlaskがGETメソッドを受け取り、ビューで許可されていないメソッドを返すことがわかります。

test.py - フラスコ

127.0.0.1 - - [04/Apr/2016 19:24:25] "GET /configure HTTP/1.1" 405 - 
から

from flask import Flask, request, session, g, redirect, url_for, \ 
    abort, render_template, flash 
import dude 

app = Flask(__name__) 


@app.route('/') 
def hello(): 
    output = None 
    if retval: 
     output = "True" 
    elif retval == False: 
     output = "False" + error 
    return render_template('index.html', output=output) 

@app.route('/configure', methods=['POST']) 
def configure(): 
    hostname = request.form['hostname'] 
    if hostname in ip_dict: 
     _info.append(hostname) 
     flash('good job') 
    else: 
     flash('Host name not in the DNS system') 
    return render_template('configure.html') 

if __name__== "__main__": 
    WB = dude.Dude() 
    retval, error, ip_dict = WB.get_ips() 
    app.run(debug=True) 

index.htmlを

<!doctype html> 
<title>configurator</title> 
<link rel=stylesheet type=text/css href="{{ url_for('static', filename='style.css') }}"> 
<div class=page> 
    <h1>Configurator</h1> 
    <div class=metanav> 
     <p>{{ output }}</p> 
     <a href="{{ url_for('configure') }}">Configure</a> 
    </div> 
    {% for message in get_flashed_messages() %} 
     <div class=flash>{{ message }}</div> 
    {% endfor %} 
    {% block body %}{% endblock %} 
</div> 

configure.html

{% extends "index.html" %} 
{% block body %} 
    <h1>Configure</h1> 
    <form action="{{ url_for('configure') }}" method=post class=hostname> 
    <d1> 
     <dt>Host name: 
     <dd><input type=text name=hostname size=15> 
     <dd><input type=submit value=Submit> 
    </d1> 
    </form> 
    {% for message in get_flashed_messages() %} 
     <div class=flash>{{ message }}</div> 
    {% endfor %} 
</div> 
{% endblock %} 

とコンソール出力

更新

私の間違いが見つかりました。私は代わりにconfigure.htmlをレンダリングするべき場所にテンプレートindex.htmlをレンダリングしています。この後、configure()の中で、代わりにリダイレクトする必要のあるテンプレートをレンダリングしています。

これは、新しいメソッドの外観です。

@app.route('/') 
def hello(): 
    output = None 
    if retval: 
     output = "True" 
    elif retval == False: 
     output = "False" + error 
    return render_template('configure.html', output=output) 

@app.route('/comeon', methods=['POST']) 
def configure(): 
    hostname = request.form['hostname'] 
    if hostname in ip_dict: 
     _info.append(hostname) 
     flash('good job') 
    else: 
     flash('Host name not in the DNS system') 
    return redirect(url_for('hello')) 
+0

あなたのHTMLの修正から始めることができます。属性値は引用符で囲む必要があります。 –

+0

あなたのコメントをありがとうございますが、これは私の問題の解決策ではありませんでした。 –

+0

@KlausD。それはちょうど真実ではないフラットです。 –

答えて

2

は、その後、あなたはGETメソッドを使用して、デフォルトでブラウザにそれを/configureを開こうとしています。 そのURLにPOSTしか指定していないので、エラーが発生しています。

フォームにいくつか問題があります。

render_template('configure.html') 

テンプレート<dd><input type=text name=hostname size=15>でそれを使用しようとしているので、あなたは、このようなhostnameなど、いくつかの引数を渡す必要があります。

これは誤った構文です。代わりに:

<dd><input type=text name={{ hostname }} size=15> 

などです。

+0

こんにちは、お返事ありがとうございます。 残念ながら、私はPythonからテンプレートに変数を渡そうとしていません。 私は入力フォームにホスト名を書いて、それをpythonに渡し、ホスト名がDNSにあるかどうかを評価しようとしています。 とにかく、あなたが提案した変更を適用しようとしましたが、エラーは変わりません。 –

+0

@VictorDaskalov(彼はあなたがブラウザで '/ configure'ビューを開くことができないと言っています)。あなたはそれをPOSTすることしかできません(つまりホームページのフォームを使用します)。 '/ configure'ページに移動するには、 'methods'に 'GET'を追加します。 –

関連する問題