简体   繁体   English

将变量从一个 function 传递到另一个 Flask 时出现类型错误

[英]TypeError when passing a variable from one function to another in Flask

I have created 2 routes in my Flask App that takes in value from the first function and then the further operations are done in the second route but I get an Error stating TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'我在我的 Flask 应用程序中创建了 2 条路由,这些路由从第一个 function 获取值,然后在第二条路由中完成进一步的操作,但我收到一个错误,说明TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'

Here is my routes.py file这是我的 routes.py 文件

@app.route('/getdetails', methods=['GET', 'POST'])
def getdetails():
    if request.method == 'POST':
        name = request.form['name']
        sems = request.form['sems']
        sems=int(sems)
        return render_template('new.html', n=name, s=sems)
    return render_template('index.html')

@app.route('/getmarks', methods=['GET', 'POST'])
def getmarks():
    sems = request.args.get('sems')
    sems = int(sems)
    for i in range(sems):
        if request.method == 'POST':
            sgp = request.form['sgpa']
            sgp = int(sgp)
            marks.append(sgp)
    return render_template('getmarks.html', s=sems, marks=marks)

also here is the html template这里还有 html 模板

{% block content %}

<form name="inputmarks" action="/getmarks" method="POST" style="width: 500px; margin: auto">
    {% for i in range(s) %}
        <label>SGPA {{i}}: </label>
        <input type="text" name="sgpa" class="form-control">
        <input type="submit" value="Lets Go">
    {% endfor %}    
</form>

The variable that I am requesting is of type 'int' but then when I print the type it shows me type as 'None Type' How do I fix this?我请求的变量是“int”类型,但是当我打印该类型时,它显示我的类型为“无类型”我该如何解决这个问题?

variable sems is the value I am requesting for变量 sems 是我要求的值

The error is probably arising from the second of these two lines:该错误可能是由这两行中的第二行引起的:

sgp = request.form['sgpa']
sgp = int(sgp)

The submitted form is either empty or is not passing in the text value.提交的表单要么是空的,要么没有传入文本值。

Try adding a print statement to check the first value of sgp尝试添加打印语句来检查sgp的第一个值

sgp = request.form['sgpa']
print('sgp =', sgp)
sgp = int(sgp)

The problem is, most likely, in the begining of the getmarks function.问题很可能出在getmarks function 的开头。

@app.route('/getmarks', methods=['GET', 'POST'])
def getmarks():
    sems = request.args.get('sems')
    sems = int(sems)

The way you are retrieving the value sems is, most likely, inconsistent with the the form you provided, and as a result, sems ends up being None , leading to the error in the call to int() .您检索值sems的方式很可能与您提供的形式不一致,因此, sems最终为None ,导致调用int()时出错。

Why does this happen?为什么会这样? It is because posted data is not persistent across HTTP calls.这是因为发布的数据在 HTTP 调用中不是持久的。 The fact the the request handled by getdetails had the value of the field sems posted to it (I'm assuming it was, even though you did not provide the HTML form for that), does not mean that the same value would automatically get passed (either as a value in form or args ) to subsequent HTTP requests, including the one handled by getmarks . getdetails 处理的请求sems getdetails值(我假设它是,即使您没有为此提供 HTML 表单),并不意味着相同的值会自动通过(作为formargs中的值)到后续的 HTTP 请求,包括由getmarks处理的请求。

You need to embed any arguments posted from the first form, which you want to be posted by the second form, inside the second form.您需要在第二个表单中嵌入从第一个表单发布的任何 arguments,您希望通过第二个表单发布。 You can do this in several ways:您可以通过多种方式执行此操作:

  • You can create an invisible field in the second form, with the same name as it had in the first form, and set it to an initial value equal to the value posted by the first request.您可以在第二个表单中创建一个不可见字段,其名称与第一个表单中的名称相同,并将其设置为与第一个请求发布的值相等的初始值。 This would require that you access this field via the form property of the Flask request, rather than through the args property.这将要求您通过 Flask 请求的form属性访问此字段,而不是通过args属性。
  • You can make the value part of the form target as a GET-style argument.您可以将表单目标的值部分作为 GET 样式的参数。 I would not recommend this method, though.不过,我不会推荐这种方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM