意外的缩进错误,但缩进看起来是正确的
Unexpected indentation error, but indentation looks correct
我一直在尝试 运行 这段代码,但它抛出了一个缩进错误。无论我怎么尝试,结果都是一样的。
如果我删除 def __str__(self):
和其余代码之前的缩进,它可以正常工作,但在输出时,它不会显示问题,而是显示 '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
我猜你在混用空格和制表符....
您可以使用 autopep 缩进代码
您正在混合使用 space 和制表符。假设 post 中的代码使用的缩进字符与您在现实中使用的缩进字符相同,下面是您的代码实际缩进的方式,>---
代表一个制表符,.
代表一个制表符space.
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
如您所见,您的缩进不一致。当定义 __str__()
的两个实例时,您现有的缩进级别是 4 spaces,但函数定义缩进了 1 个制表符。这会导致错误。
按照惯例,Python 代码只能使用 space 缩进,而不能使用制表符,正是出于这个原因。
另见 PEP 8, specifically the sections "Indentation" and "Tabs or Spaces?
我一直在尝试 运行 这段代码,但它抛出了一个缩进错误。无论我怎么尝试,结果都是一样的。
如果我删除 def __str__(self):
和其余代码之前的缩进,它可以正常工作,但在输出时,它不会显示问题,而是显示 '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
我猜你在混用空格和制表符....
您可以使用 autopep 缩进代码
您正在混合使用 space 和制表符。假设 post 中的代码使用的缩进字符与您在现实中使用的缩进字符相同,下面是您的代码实际缩进的方式,>---
代表一个制表符,.
代表一个制表符space.
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
如您所见,您的缩进不一致。当定义 __str__()
的两个实例时,您现有的缩进级别是 4 spaces,但函数定义缩进了 1 个制表符。这会导致错误。
按照惯例,Python 代码只能使用 space 缩进,而不能使用制表符,正是出于这个原因。
另见 PEP 8, specifically the sections "Indentation" and "Tabs or Spaces?