【问题标题】:I am getting an Unexpected Indent error in a nested if block我在嵌套的 if 块中收到 Unexpected Indent 错误
【发布时间】:2011-06-01 00:56:30
【问题描述】:

我正在使用嵌套的 if 块来决定适当的视图。但是在 if 块之后我收到了 Unexpected Indent 错误。我无法找出我在缩进中犯错的地方。

def logged_home(request):
        names = request.user.social_auth.values_list('provider', flat=True)
        ctx = dict((name.lower().replace('-', '_'), True) for name in names)
        ctx['version'] = version
        return render_to_response('logged_home.html', ctx, RequestContext(request))


def home(request):
        if request.user.is_authenticated():
                profile = user.get_profile()
                if profile.is_new == True:
                        return welcome(request)
                else:
                        return logged_home(request)
        else:
                return not_logged_home(request)


def signup(request):
        return render_to_response('signup.html', {}, context_instance = RequestContext(request))

【问题讨论】:

  • 您确定缩进错误不在 not_logged_homelogged_home 方法/视图中吗?
  • 是的,我确定 not_logged_homelogged_home 中没有错误,因为在我编辑主视图之前它们执行时没有出现错误。
  • 我建议在vim 中使用:set list 或在其他文本编辑器中使用类似的命令来向您展示制表符和空格之间的区别。您的 IDE 可能配置为始终使用制表符,但很容易错误配置空格/制表符。
  • 你为什么使用混合的 4 和 8 缩进?
  • @tmg 我想我把 vim 和 gedit 弄错了。

标签: python django django-views


【解决方案1】:

您可能正在混合制表符和空格,并且没有将制表符大小设置为 8。

【讨论】:

  • 我没有混合制表符和空格。我只使用制表符进行缩进。
  • 使用一个可以让你看到不可见字符的编辑器,这样你就可以调试它。
  • 我使用了@sarnold 提到的vim 命令,并修改了代码以仅使用制表符。修改后的代码在this link
  • @moving 您正在粘贴的代码不是您正在使用的代码,请参阅dpaste.com/548980 如何在第 2 行我有两个选项卡,在第 3 行我有 4 个空格。现在将其与您的粘贴进行比较。
  • 错误出现在我在这里导入的另一个视图文件中。该文件有 4 个空格缩进,这可能是导致错误的原因。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多