Python 缩进问题 : Expected an indentation block

标签 python django

def process_health_case(request):
    about = request.POST['about']
    details = request.POST['narrative_text']
    image=request.FILES['image_scan']
    connection = Connection('mongodb://sbose78:PASSWORD@staff.mongohq.com:10068/BOSE')
    if images:
        db=connection['BOSE']
        fs=gridfs.GridFS(db)
        fs.put(image,filename="image_scan2")
    else:
        #nothing
    return render_to_response('home/new_narrative.html',{ }, context_instance=RequestContext(request))

我得到了

expected an indented block (views.py, line 41)

第 41 行是最后一行。

我哪里错了?

谢谢。

最佳答案

您不能将评论用作空语句,您应该使用 pass 如果你想要一个什么都不做的显式 else。

if images:
    db=connection['BOSE']
    fs=gridfs.GridFS(db)
    fs.put(image,filename="image_scan2")
else:
    pass
return ....

由于没有声明,所以在 else 中发表评论在你的代码中,python 认为 return应该是else的内容并给出意图错误。

关于Python 缩进问题 : Expected an indentation block,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12328709/

相关文章:

python - 使用 for 循环在 django 模板中显示值

python - 在 Tensorflow 中训练 wordvec,导入到 Gensim

python - 使用 xarray 添加全局属性

python - TensorFlow 中出现错误

python - 用于性能、缓存的 Numpy 纯函数

python - 多表单处理 Django python 3.x

python - 什么是实现多个构造函数的干净 "pythonic"方式?

python - 是否可以在生成 500 时向 django 发送给管理员的电子邮件添加 header ?

ruby-on-rails - 重新编码播客网站 - 我应该使用什么?

Django runserver 自定义数据库