【发布时间】:2019-07-31 15:55:43
【问题描述】:
以下是 4 个文件,每个文件由一系列“#”磅或 hashTag 符号分隔,后跟文件名。 代码位于重音符号 ( ` ) 之间。
问题是当这个 python 驱动的 web 应用程序执行时,python 文件“evenOrOdd.py”中的第 11 行是发生错误的地方。 当您在 Web 浏览器上转到: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'
【问题讨论】:
-
request.form.get("numberInputFromForm")这是返回无。 -
html 文件 "evenOrOdd.html" 和 html 文件 "index.html" 中的代码链接
<form action="{{url_for('indexFunction')}}" method="post">```` should return a valid integer number on considering it contains```请帮助我理解为什么它返回一个 NoneType -
尝试打印它(不转换为 int),然后再转换为 int 看看你得到了什么。
标签: python html css flask jinja2