[英]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 表单),并不意味着相同的值会自动通过(作为form
或args
中的值)到后续的 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:
您可以通过多种方式执行此操作:
form
property of the Flask request, rather than through the args
property.form
属性访问此字段,而不是通过args
属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.