python - 如何修复,TypeError : unsupported operand type(s) for %: 'NoneType' and 'int'

标签 python html css flask jinja2

以下是 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.py
from 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")

关于python - 如何修复,TypeError : unsupported operand type(s) for %: 'NoneType' and 'int' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57294755/

相关文章:

python - 比较 2 个列表并将索引和值返回给第三个列表

python - 数据从文本文件读取到python中的两个列表

javascript - 将 PDF 保存到服务器而不是客户端计算机上

javascript - JQuery 导航集类

html - 在 OSX Chrome 上打印 HTML 页面对齐

python - 如何从集合中检索元素而不删除它?

python - 建立新连接失败: [Errno -2] Name or service not known

android - 在 inappbrowser Phonegap webview 应用程序顶部显示自定义导航

c# - 显示表格的所有边框线

php - 在 php 变量中添加样式