请求的URL不允许使用该方法。在烧瓶中

时间:2015-04-05 19:59:49

标签: python flask

我写这个论坛,我想验证用户名和密码,但我有结果.Method Not Allowed

hello.html的



<html lang='en'>
<head>
    <meta charset="utf-8" />
    <title>Hello</title>

</head>
{% if error %}
    {{ error }}
{% endif %}
<body>

      Hello 

</body>
</html>
&#13;
&#13;
&#13;

请求的网址不允许使用该方法。

<!DOCTYPE html>
<!--[if (gte IE 9)|!(IE)]><!--> <html lang="en"> <!--<![endif]-->
<head>
<meta charset="utf-8">
<title>welcome</title>
<link href="{{ url_for('static', filename='style.css') }}" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="container">
    <section id="content">
        <form action="/login" method="post">
            <h1>Login Form</h1>
            <div>
                <input type="text" placeholder="Username" required=""  name="username" />
            </div>
            <div>
                <input type="password" placeholder="Password" required="" name="password" />
            </div>
            <div>
                <input type="submit" value="Log in"  />

            </div>
        </form><!-- form -->

    </section><!-- content -->

</div><!-- container -->
</body>
</html>

我写了一个论坛:

在app.py中

from flask import Flask
from flask import request
from flask import render_template, redirect, url_for, request
APP = Flask(__name__)
@APP.route('/')
def login1():
      return render_template('login1.html')
@APP.route('/login', methods=['GET', 'POST'])
def login():
    error = None
    if request.method == 'POST':
        if request.form['username'] != 'admin' or request.form['password'] != 'admin':
            error = 'Invalid Credentials. Please try again.'
        else:
            return redirect(url_for('home'))
   return render_template('hello.html', error=error)

if __name__ == '__main__':
      APP.debug=True
      APP.run()

我无法验证输入? 怎么可以帮助我

2 个答案:

答案 0 :(得分:3)

我将您的代码app.py代码修改为以下内容,以及您当前的hello.html和login1.html,这是正常的。

from flask import Flask
from flask import request
from flask import render_template, redirect, url_for, request
APP = Flask(__name__)
@APP.route('/')
def login1():
      return render_template('login1.html')
@APP.route('/login', methods=['GET', 'POST'])
def login():
    error=None
    if request.method == 'POST':
        if request.form['username'] != 'admin' or request.form['password'] != 'admin':
            error = 'Invalid Credentials. Please try again.'
            return redirect(url_for('login1'))
        else:
           return render_template('hello.html', error=error)

if __name__ == '__main__':
      APP.debug=True
      APP.run()

答案 1 :(得分:0)

试试这个。

首先更改app.py

中的行
@APP.route('/?', methods=['GET', 'POST']) 

@APP.route('/login', methods=['GET', 'POST'])

然后在login1.html中,将这些行更改为相应的行。

<form action="/login" method="post">

<input type="text" placeholder="Username" required="" id="username" name="username" />

<input type="password" placeholder="Password" required="" id="password" name="password" />

<强>更新

    <form action="/login" method="post">
        <h1>Login Form</h1>
        <div>
            <input type="text" placeholder="Username" required="" id="username" name="username" />
        </div>
        <div>
            <input type="password" placeholder="Password" required="" id="password" name="password" />
        </div>
        <div>
            <input type="submit" value="Log in"  />

        </div>
    </form><!-- form -->

更新2 hello.html

<html lang='en'>
<head>
    <meta charset="utf-8" />
    <title>Hello</title>

</head>
{% if error %}
    {{ error }}
{% endif %}
<body>

      Hello 

</body>
</html>