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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29461615/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 04:36:14  来源:igfitidea点击:

The method is not allowed for the requested URL. in Flask

pythonflask

提问by malek tio

I write this forum and I want to validate user name and password but I have result.Method Not Allowed

我写这个论坛,我想验证用户名和密码,但我有结果。方法不允许

hello.html

你好.html

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

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

      Hello 

</body>
</html>

The method is not allowed for the requested URL.

所请求的 URL 不允许使用该方法。

<!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>

I write a forum:

我写一个论坛:

in app.py:

在 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()

I can't validate the input? how can help me please

我无法验证输入?请如何帮助我

采纳答案by LeoG

I modified your code app.py code to the below, along with your current hello.html and login1.html this is working fine.

我将您的代码 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()

回答by itzMEonTV

Try this.

尝试这个。

First change the line in app.py

首先改变行 app.py

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

to

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

And then in login1.html, change these lines to corresponding lines.

然后在 中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" />

Update

更新

    <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 -->

Update 2hello.html

更新 2hello.html

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

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

      Hello 

</body>
</html>