为什么在Django示例教程中会出现Python缩进错误?
我从Django的示例教程中拿到了这段代码。
from django.db import models
from datetime import datetime
# Create your models here.
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_today(self):
return self.pub_date.date() == datetime.date.today()
但是我遇到了这个错误:IndentationError: unexpected indent。
错误出现在这一行:
def __unicode__(self):
有没有人知道哪里出问题了??
4 个回答
1
有时候,处理大文件时很难找到错误的地方,这时候你可以安装一个叫做pep8的模块,链接在这里:http://pypi.python.org/pypi/pep8,然后可以通过命令行来使用它。
$ pep8 a.py a.py:1:4: W191 indentation contains tabs a.py:1:4: E101 indentation contains mixed spaces and tabs a.py:1:5: E113 unexpected indentation
3
可能是因为你从教程里复制粘贴了代码。要特别注意制表符和空格的使用。
希望这能帮到你。
12
我猜可能是你在代码里混用了制表符和空格。
一般建议使用空格,而不是制表符,每次缩进用4个空格比较好。http://www.python.org/dev/peps/pep-0008/
你在编辑器里有没有显示不可见字符,这样可以确认是不是这个问题呢?
PS:你的信誉图上显示-6000,这让我有点困扰。