以下是 4 个文件,每个文件由一系列“#”井号或 hashTag 符号分隔,后跟文件名。代码位于重音符 (`) 符号之间。
问题是当这个由 python 驱动的 web 应用程序被执行时,python 文件“evenOrOdd.py”中的第 11 行是发生错误的地方。当您转到:http : //127.0.0.1 : 5000在您的网络浏览器上出现以下错误时:
TypeError: unsupported operand type(s) for %: 'NoneType' and 'int'
单击此错误时,它会将我带到第 11 行。
在第 11 行,python 代码是:-
elif num%2==0:
第 7 行是变量“num”,定义为:-
num=int(request.form.get("numberInputFromForm"))
我已经尝试删除 python 文件“evenOrOdd.py”的第 7 行的 int(),不起作用,仍然给出相同的错误。
而且我还独立尝试将“num”变量转换为 int()。
############################################evenOrOdd.pyfrom flask import Flask, render_template, request
app=Flask("__name__")
@app.route("/", methods=["POST", "GET"])
def indexFunction():
num=int(request.form.get("numberInputFromForm"))
dict={'even':False, 'odd':False, 'zero':False, 'number_input':num}
if num==0:
dict['zero']=True
elif num%2==0:
dict['even']=True
else:
dict['odd']=True
if request.method=="POST":
return render_template("evenOrOdd.html", dict=dict)
return render_template("index.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
{% 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
{% 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 %}
发生以下错误:- TypeError:% 不支持的操作数类型:'NoneType' 和 'int'
要将表单(我假设在“index.html”中)放入浏览器,有一个初始 GET。除非你通过?numberInputFromForm=something
,numberInputFromForm
否则不会出现,并且会以None
.
修复是保护依赖于它的代码路径,这样路径只在 POST 时被采用。就像是
if request.method == 'POST':
num=int(request.form.get("numberInputFromForm"))
...
return render_template("evenOrOdd.html", ...)
else:
return render_template("index.html")
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句