Python类方法定义:“意外缩进"; [英] Python Class method definition : "unexpected indent"

查看:53
本文介绍了Python类方法定义:“意外缩进";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始使用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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆