Flask:Expected an indented block

时间:2015-12-14 18:13:00

标签: python flask

So I am getting an error in my flask app which says:

File app.py, line 13
first = request.form.get('first')

IndentationError: expected an indented block

Here is my app.py code

import time
import requests
import requests_cache

from flask import Flask, render_template, request, jsonify

app = Flask(__name__)



@app.route('/', methods=['GET', 'POST'])
def home():
    if request.method == 'POST':
        first = request.form.get('first')
        url = "http://myAPIURL.com/ws/spm/search/perfid/{0}".format(first)
        response = requests.get(url)
        return jsonify(response.json())
    return render_template('index.html')


if __name__ == '__main__':
    app.run(debug=True)

I have checked the indentations and they seem to be fine to me,

Can someone please point out what I am doing wrong?

1 个答案:

答案 0 :(得分:0)

问题当然在于混合标签和空格。您应该始终使用空格缩进并更改编辑器上的设置以使用空格而不是制表符,这样您的缩进将在不同的系统/编辑器之间保持一致。

但是如果您希望使用制表符字符来真正缩进代码,至少要坚持'永远不要混合制表符和空格' 的黄金法则。不要在你的代码上使用它们,因为它会导致错误和可能不需要的行为(例如,一行最终会在不同的块下缩进)

PEP8强烈建议使用空格。 this question是关于为何推荐它的讨论。

相关问题