【问题标题】:How to fix, TypeError: unsupported operand type(s) for %: 'NoneType' and 'int'如何修复,TypeError: 不支持的操作数类型为 %: 'NoneType' 和 'int'
【发布时间】: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.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'

【问题讨论】:

  • request.form.get("numberInputFromForm") 这是返回无。
  • html 文件 "evenOrOdd.html" 和 html 文件 "index.html" 中的代码链接 &lt;form action="{{url_for('indexFunction')}}" method="post"&gt;```` should return a valid integer number on considering it contains ```请帮助我理解为什么它返回一个 NoneType
  • 尝试打印它(不转换为 int),然后再转换为 int 看看你得到了什么。

标签: python html css flask jinja2


【解决方案1】:

为了将表单(我假设它在“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")

【讨论】:

    猜你喜欢
    • 2020-01-17
    • 2017-12-27
    • 2014-03-31
    • 2021-05-23
    • 2015-10-15
    • 2016-08-25
    • 1970-01-01
    • 2013-02-08
    • 2016-10-22
    相关资源
    最近更新 更多