为什么我在 django 示例教程中遇到 python 缩进错误
我有来自 tDjango 示例教程的代码
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:意外缩进
在这一行:
def __unicode__(self):
知道出了什么问题吗?
i have this code from the django sample tutorial
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()
and i am getting this error: IndentationError: unexpected indent
on this line:
def __unicode__(self):
any idea what is wrong ??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我唯一的猜测是你有制表符和空格混合在一起。
建议在制表符上使用空格,每个缩进 4 个空格。
http://www.python.org/dev/peps/pep-0008/
您的编辑器上是否有可见的不可见字符,以确保情况并非如此?
PS:你的声誉图显示为 -6000,这真的让我很烦恼。
My only guess is you have tabs mixed with spaces.
It's recommended to use spaces over tabs, with 4 spaces per indentation.
http://www.python.org/dev/peps/pep-0008/
Do you have invisible characters visible on your editor to make sure that isn't the case?
PS: your reputation graph says -6000 and it's really bugging me out.
可能这是因为您从教程中复制/粘贴了。
特别注意制表符和空格。
希望有帮助。
Probably this is because you did copy/paste from the tutorial.
Pay special attention to the tabs and spaces.
Hope it helps.
有时,使用大文件查找错误位置可能很困难,因此您可以安装 pep8 http://pypi .python.org/pypi/pep8 模块并从命令行使用它
Sometimes it can be hard work with big files to find where error is, so you can install pep8 http://pypi.python.org/pypi/pep8 module and use it from command line
Python INDENTION 在大多数情况下都会负责,为了正确工作,请使用编辑器或使用选项卡分离出 _unicode_(self)
Python INDENTION will be responsible most of the time, to work correclty, use the editor or separate out the _unicode_(self) with tab