Django教程:意外的缩进错误

时间:2014-08-09 03:10:25

标签: python django python-2.7 ubuntu django-models

这是我的model.py代码:

from django.db import models
# Create your models here.

class Poll(models.Model):
    question = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')
        def __str__(self):
        return self.question


class Choice(models.Model):
    poll = models.ForeignKey(Poll)
    choice_text = models.CharField(max_length=200)
    votes = models.IntegerField(default=0)
        def __str__(self):
        return self.choice_text

当我运行以下命令时:

python manage.py runserver

这给了我以下错误:

  

mjrulesamrat @ mjrulesamrat-Lenovo-G570:〜/ django_local / first_web $ python   manage.py runserver验证模型......

     

Traceback启动的线程中的未处理异常(最近一次调用最后一次):文件   “/usr/local/lib/python2.7/dist-packages/django/utils/autoreload.py”   第93行,包装中       fn(* args,** kwargs)文件“/usr/local/lib/python2.7/dist-packages/django/core/management/commands/runserver.py”,   第98行,在inner_run中       self.validate(display_num_errors = True)File“/usr/local/lib/python2.7/dist-packages/django/core/management/base.py”,   第310行,在验证中       num_errors = get_validation_errors(s,app)文件“/usr/local/lib/python2.7/dist-packages/django/core/management/validation.py”,   第34行,在get_validation_errors中       for(app_name,error)in get_app_errors()。items():File“/usr/local/lib/python2.7/dist-packages/django/db/models/loading.py”,   第196行,在get_app_errors中       self._populate()文件“/usr/local/lib/python2.7/dist-packages/django/db/models/loading.py”,   第75行,在_populate中       self.load_app(app_name,True)文件“/usr/local/lib/python2.7/dist-packages/django/db/models/loading.py”,   第99行,在load_app中       models = import_module('%s.models'%app_name)文件“/usr/local/lib/python2.7/dist-packages/django/utils/importlib.py”,   第40行,在import_module中       导入(名称)文件“/home/mjrulesamrat/django_local/first_web/polls/models.py”,第7行       def str (个体经营):       ^   IndentationError:意外缩进

我正在使用Django 1.6和python 2.7。

如果我在此代码中犯了一些错误,请指导我。因为当我在python shell中运行时,它给了我轮询对象而不是问题。

>>> Poll.objects.all()
[<Poll: Poll object>]

1 个答案:

答案 0 :(得分:1)

在模型方法级别观察/修复缩进:

from django.db import models
# Create your models here.

class Poll(models.Model):
    question = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')

    # HERE 
    def __str__(self):
        return self.question


class Choice(models.Model):
    poll = models.ForeignKey(Poll)
    choice_text = models.CharField(max_length=200)
    votes = models.IntegerField(default=0)

    # AND HERE
    def __str__(self):
        return self.choice_text