Python类方法定义:“意外缩进"; [英] Python Class method definition : "unexpected indent"
问题描述
我开始使用Django和Python,所以自然而然地我在做民意调查项目教程.我正在Windows 7下使用Python 2.7.9和Django 1.3.7
I am getting started with Django and Python so naturally I'm doing the polls project tutorial. I am working under Windows 7 with Python 2.7.9 and Django 1.3.7
我有这段代码(括号内的行号):
I have this piece of code (with line numbers under brackets):
(3) class Poll(models.Model):
(4) question = models.CharField(max_length=200)
(5) pub_date = models.DateTimeField('date published')
(6) def __unicode__(self):
(7) return self.question
,当尝试运行"manage.py shell"时,出现以下错误:
and when trying to run "manage.py shell" I get the following error :
文件"C:\ Users ... \ mysite \ polls \ models.py",第6行
File "C:\Users...\mysite\polls\models.py", line 6
def unicode (自己)
^
IndentationError:意外缩进
IndentationError: unexpected indent
我在做什么错了?
推荐答案
您的 class
标头与其内容具有相同的缩进形式,您需要优化缩进形式(4个缩进空间):
Your class
header is in same indentation with its content , you need to refine the indentation (as a pythonic way use 4 space for indentation ):
class Poll(models.Model):
question = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
def __unicode__(self):
return self.question
这篇关于Python类方法定义:“意外缩进";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!