【问题标题】:request.form.get("xxxxx") returns Nonerequest.form.get("xxxxx") 返回无
【发布时间】:2021-02-25 17:40:56
【问题描述】:

我已经找了很长时间了,但我根本无法让这个工作我两天前开始使用烧瓶和 html,但它一直没有返回

这是html代码

<!DOCTYPE html>
<html>
    <head>
        <title>Adding numbers</title>
    </head>
    <body>
        <form method="post"></form>
            
            <label>Chose your numbers</label> <br>
            
            <label> Number 1</label>
            <p><input name="location" type="text" /></p>
            
            <label> Number 2</label>
            <p><input name="location2" type="text" /></p>
            

            <label>The result is</label>
            <label>{{result}}</label><br>

            <button type="submit" name="submit">Submit</button>
        </form>

    </body>
</html>

这是python代码

from flask import Flask, render_template, url_for, redirect, request
import ops

app = Flask(__name__)

@app.route("/", methods= ["GET", "POST"])
def home():
    if request.method == "GET":
        return render_template("index.html")
    if request.method == "POST":
        o = request.form.get('op')
        if o == "+":
            return redirect(url_for("sum"))
            
                   

@app.route("/sum", methods= ["GET", "POST"])
def sum():
    s1 = request.args.get('location')
    s2 = request.args.get('location2')
    return render_template("sum.html", result= ops.adding(float(s1) + float(s2)))


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

我的问题是位置 1 和 2 简单地没有从 html 传递到 pythom 这是一张图片,因为这是我的第一篇文章,我真的不知道这个网站是如何运作的

enter image description here

【问题讨论】:

    标签: python-3.x flask python-requests


    【解决方案1】:

    您过早地关闭了表单标签 (&lt;/form&gt;),并且您正在尝试执行 POST 请求并尝试使用 Python 代码上的 GET 参数访问值。

    修复了 HTML,它将表单值作为 GET 参数发送,例如/sum?location=x&amp;location2=y:

    <!DOCTYPE html>
    <html>
        <head>
            <title>Adding numbers</title>
        </head>
        <body>
    
            <form method="GET" action="{{ url_for('sum') }}">
                
                <label>Chose your numbers</label> <br>
                
                <label> Number 1</label>
                <p><input name="location" type="text" /></p>
                
                <label> Number 2</label>
                <p><input name="location2" type="text" /></p>
                
    
                <label>The result is</label>
                <label>{{result}}</label><br>
    
                <button type="submit" name="submit">Submit</button>
            </form>
    
        </body>
    </html>
    

    /sum路由:

    @app.route("/sum")
    def sum():
        s1 = request.args.get('location')
        s2 = request.args.get('location2')
        if s1 and s2:
            return render_template("sum.html", result= ops.adding(float(s1) + float(s2)))
        return render_template("sum.html")
    

    request.args 文档:https://flask.palletsprojects.com/en/1.1.x/api/#flask.Request.args

    【讨论】:

    • 我非常感谢你,但是“如果 s1 和 s2:”有什么意义呢
    • 简单检查以确保s1s2 的值不是None,尝试将这些值相加是没有意义的,其中一个是None
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-21
    • 1970-01-01
    • 2019-04-26
    • 1970-01-01
    • 2018-11-18
    • 1970-01-01
    相关资源
    最近更新 更多