意外的缩进错误,但缩进看起来正确 [英] Unexpected indentation error, but indentation looks correct

查看:108
本文介绍了意外的缩进错误,但缩进看起来正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试运行此代码,并且抛出缩进错误.不管我尝试什么,结果都是一样的.

I have been trying to run this code and it throws an indent error. No matter what I try, the result is the same.

如果我删除def __str__(self):之前的缩进和其余代码,它可以正常工作,但是在输出时,它不显示问题,而是显示"Question object".

If I delete the indent before def __str__(self): and the rest of the code, it works fine, but on output, instead of displaying the question, it shows 'Question object'.

def __str__(self):
^
IndentationError: unexpected indent

这是代码:

from __future__ import unicode_literals
from django.db import models
from django.utils.encoding import python_2_unicode_compatible
from django.utils import timezone

class Question(models.Model):
    question_text = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')

    def __str__(self):
        return self.question_text

    def was_published_recently(self):
        return self.pub_date >= timezone.now() - datetime.timedelta(days=1)

class Choice(models.Model):
    question = models.ForeignKey(Question, on_delete=models.CASCADE) 
    choice_text = models.CharField(max_length=200)
    votes = models.IntegerField(default=0)

    def __str__(self):
        return self.choice_text

推荐答案

我猜您正在混合使用空格和制表符....

I guess you are mixing spaces and tabs ....

您可以使用autopep缩进代码

You can indent the code using autopep

请参见 https://pypi.python.org/pypi/autopep8

这篇关于意外的缩进错误,但缩进看起来正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆