[英]How to fix, TypeError: unsupported operand type(s) for %: 'NoneType' and 'int'
Following are 4 files, each separated by a series of "#" pound or hashTag symbols followed by the file name. 以下是4个文件,每个文件由一系列“#”井号或hashTag符号分隔,后跟文件名。 The code is between grave accent ( ` ) symbols. 该代码位于重音符号(`)之间。
Problem is that when this python powered web application is executed, the line number 11 in python file "evenOrOdd.py" is where the error is occuring. 问题是执行此python供电的Web应用程序时,发生错误的位置是python文件“ evenOrOdd.py”中的行号11。 When you goto: http://127.0.0.1:5000 on your web browser following error occurs: 当您在Web浏览器上转到: http : //127.0.0.1 : 5000时 ,发生以下错误:
TypeError: unsupported operand type(s) for %: 'NoneType' and 'int'
On clicking this error, it takes me to the line number 11. 点击此错误后,我进入第11行。
On line number 11, python code is:- 在第11行,python代码为:-
elif num%2==0:
On line number 7 is the variable "num" defined as:- 在第7行上,变量“ num”定义为:
num=int(request.form.get("numberInputFromForm"))
I have tried removing the int() on line 7 of python file "evenOrOdd.py", doesn't work, still gives the same error. 我试过删除python文件“ evenOrOdd.py”的第7行上的int(),不起作用,仍然给出相同的错误。
And also I've independently tried to convert "num" variable to int(). 而且我也独立尝试将“ num”变量转换为int()。
###############################################evenOrOdd.py ################################################ evenOrOdd.py{% extends "layout.html" %}
{% block f %}
<form action="{{url_for('indexFunction')}}" method="post">
<input name='numberInputFromForm' type='number' placeholder="Enter number here...">
<button> Submit </button>
</form>
{% endblock %}
###############################################layout.html
############################################## layout.html
<!DOCTYPE html> <html> <head> <title>My Web Page</title> <link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}"> </head> <body> <br> <h1>Python powered web app<h1> This application tells is a number is even or odd <br> {% block f %} {% endblock %} <p> {% block p %} {% endblock %} </p> </body> </html>
###############################################index.html
############################################## index.html
{% extends "layout.html" %} {% block f %} <form action="{{url_for('indexFunction')}}" method="post"> <input name='numberInputFromForm' type='number' placeholder="Enter number here..."> <button> Submit </button> </form> {% endblock %}
###############################################evenOrOdd.html
################################################ evenOrOdd.html
{% extends "layout.html" %} {% block p %} {% if dict['even'] %} <h1> {{dict['number_input']}} is EVEN </h1> {% elif dict['odd'] %} <h1> {{dict['number_input']}} is ODD </h1> {% else %} <h1> {{dict['number_input']}} is ZERO </h1> {% endif %} {% endblock %} {% block f %} <form action="{{url_for('indexFunction')}}" method="post"> <input name='numberInputFromForm' type='number' placeholder="Enter number here..."> <button> Submit </button> </form> {% endblock %}
Following error occurs:- TypeError: unsupported operand type(s) for %: 'NoneType' and 'int' 发生以下错误:-TypeError:%不支持的操作数类型:'NoneType'和'int'
To get the form (which I assume is in "index.html") into the browser, there's an initial GET. 为了将表单(我认为在“ index.html”中)放入浏览器,需要进行初始GET。 Unless you pass ?numberInputFromForm=something
, numberInputFromForm
won't get present, and will present as None
. 除非您通过?numberInputFromForm=something
, numberInputFromForm
将不会显示,并且将显示为None
。
The fix is to guard the code path that depends on it, such that the path is only taken on a POST. 解决方法是保护依赖它的代码路径,以使该路径仅在POST上采用。 Something like 就像是
if request.method == 'POST':
num=int(request.form.get("numberInputFromForm"))
...
return render_template("evenOrOdd.html", ...)
else:
return render_template("index.html")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.