提问者:小点点

我不明白为什么我的Flask代码没有连接到我的HTTP方法


这是我的Flask代码,它应该打开另一个页面,欢迎从我的html代码登录的用户

from flask import Flask, redirect, url_for, request,render_template
app = Flask(__name__)

@app.route('/')
def home():
    return render_template('login.html')

@app.route('/success/<name>')
def success(name):
    return ('Welcome %s' % name)

@app.route('/login', methods = ['POST','GET'])
def login():
    if request.method == 'POST':
        user = request.form['nm']
        return (redirect(url_for(' success' , name = user)))
    else:
        user = request.args.get('nm')
        return (redirect(url_for('success', name = user)))

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

这是我的html代码

    <html>
    <body>
        <form action="http://localhost:5000/login" method="post" >
            <p>Enter Name: </p>
            <p><input type = "text" name = "nm"/></p>
            <p><input type = "submit" value="submit" /></p>

        </form>
    </body>
</html>

html代码在终端中给出了此错误

错误:Get_Updates_Processor.cc(258)]在GetUpdates期间PostClientToServerMessage()失败,错误为%2


共1个答案

匿名用户

少数错误:

  1. 对于POST,success中有一个额外的空格。
  2. __main__未正确对齐。

这是固定代码

from flask import Flask, redirect, url_for, request,render_template
app = Flask(__name__)

@app.route('/')
def home():
    return render_template('login.html')

@app.route('/success/<name>')
def success(name):
    return ('Welcome %s' % name)

@app.route('/login', methods = ['POST','GET'])
def login():
    if request.method == 'POST':
        user = request.form['nm']
        return (redirect(url_for('success' , name = user)))
    else:
        user = request.args.get('nm')
        return (redirect(url_for('success', name = user)))

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