谁能帮我解决python flask Indentation错误?

问题描述 投票:1回答:1

实际上是在文件“ D:\ app \ app \ views.py”的第11行说缩进错误@ app.route(“ / api / info”)^IndentationError:unindent与任何外部缩进级别都不匹配从应用程序导入应用程序

from flask import render_template, request
import time

@app.route("/")
def index():
    return render_template("index.html")


 @app.route("/api/info")
def api_info():
    info = {
       "ip" : "127.0.0.1",
       "hostname" : "everest",
       "description" : "Main server",
       "load" : [ 3.21, 7, 14 ]
    }
    return jsonify(info)

@app.route("/api/calc")
def add():
    a = int(request.args.get('a', 0))
    b = int(request.args.get('b', 0))
    div = 'na'
    if b != 0:
        div = a/b
    return jsonify({
        "a"        :  a,
        "b"        :  b,
        "add"      :  a+b,
        "multiply" :  a*b,
        "subtract" :  a-b,
        "divide"   :  div,
    })
python flask syntax-error indentation
1个回答
-1
投票

是的,先生,第11行您有缩进错误:

from flask import render_template, request
import time

@app.route("/")
def index():
    return render_template("index.html")

# INDENTATION ERROR HERE ON START OF NEXT LINE
 @app.route("/api/info")
def api_info():
    info = {
       "ip" : "127.0.0.1",
       "hostname" : "everest",
       "description" : "Main server",
       "load" : [ 3.21, 7, 14 ]
    }
    return jsonify(info)

@app.route("/api/calc")
def add():
    a = int(request.args.get('a', 0))
    b = int(request.args.get('b', 0))
    div = 'na'
    if b != 0:
        div = a/b
    return jsonify({
        "a"        :  a,
        "b"        :  b,
        "add"      :  a+b,
        "multiply" :  a*b,
        "subtract" :  a-b,
        "divide"   :  div,
    })

Just remove the extra beginning 2 spaces on the line with '@app.route("/api/info")'.

将来,使用带有行号的IDE将帮助您缩小这种错误的范围。

© www.soinside.com 2019 - 2024. All rights reserved.