【问题标题】:Django tutorial: unexpected indent errorDjango 教程:意外缩进错误
【发布时间】:2014-10-02 14:30:45
【问题描述】:

这是我的 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) 文件 "/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(): 文件 "/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 中 模型 = 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 中运行 follow 时,它给了我 poll 对象而不是问题。

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

【问题讨论】:

  • 阅读错误的最后一行:File "/home/mjrulesamrat/django_local/first_web/polls/models.py", line 7 def str(self): ^ IndentationError: unexpected indent。意外缩进,第 7 行。
  • 修复它很容易。不要使用空格键,而是使用制表符。对了,在 def __str__(self): 之后检查你的意图
  • 请不要贬低我的问题。我对 django 还很陌生,所以我犯了这个错误。收回那个。

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


【解决方案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

【讨论】:

    猜你喜欢
    • 2012-10-25
    • 2011-04-24
    • 2021-05-28
    • 2014-03-15
    • 2019-10-31
    • 1970-01-01
    • 1970-01-01
    • 2022-04-04
    • 1970-01-01
    相关资源
    最近更新 更多