Reputation: 7681
I am trying to use this code:
class Poll(models.Model):
question = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
def __unicode__(self):
return self.question
def was_published_recently(self):
return self.pub_date >= timezone.now() - datetime.timedelta(days=1)
but I am getting an unexpected indent error at the 'def unicode' line, however to me this looks correct. Does anybody know why?
Upvotes: 1
Views: 89
Reputation: 992707
You are mixing tabs and spaces on different lines, which is causing this problem. Use spaces only (and turn off tabs in your editor).
Specifically, it appears that your pub_date
line is indented with 4 spaces, but the def __unicode__
line is indented with a tab. Python does not know what your tab value is set to in your editor, so it makes the assumption that a tab means an 8 space indent. The easiest (and recommended) way to avoid this is to only use spaces.
From PEP 8:
Tabs or Spaces?
Spaces are the preferred indentation method.
Tabs should be used solely to remain consistent with code that is already indented with tabs.
Python 3 disallows mixing the use of tabs and spaces for indentation.
Python 2 code indented with a mixture of tabs and spaces should be converted to using spaces exclusively.
When invoking the Python 2 command line interpreter with the -t option, it issues warnings about code that illegally mixes tabs and spaces. When using -tt these warnings become errors. These options are highly recommended!
Upvotes: 2