gpt4 book ai didi

python - django教程: unexpected indent error

转载 作者:行者123 更新时间:2023-12-04 19:19:33 25 4
gpt4 key购买 nike

这是我的 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 Validating models...

Unhandled exception in thread started by Traceback (most recent call last): File "/usr/local/lib/python2.7/dist-packages/django/utils/autoreload.py", line 93, in wrapper fn(*args, **kwargs) File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/runserver.py", line 98, in inner_run self.validate(display_num_errors=True) File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 310, in validate num_errors = get_validation_errors(s, app) File "/usr/local/lib/python2.7/dist-packages/django/core/management/validation.py", line 34, in 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", line 196, in get_app_errors self._populate() File "/usr/local/lib/python2.7/dist-packages/django/db/models/loading.py", line 75, in _populate self.load_app(app_name, True) File "/usr/local/lib/python2.7/dist-packages/django/db/models/loading.py", line 99, in load_app models = import_module('%s.models' % app_name) File "/usr/local/lib/python2.7/dist-packages/django/utils/importlib.py", line 40, in import_module import(name) File "/home/mjrulesamrat/django_local/first_web/polls/models.py", line 7 def str(self): ^ IndentationError: unexpected indent



我正在使用 Django 1.6 和 python 2.7。

如果我在此代码中犯了一些错误,请指导我。因为当我在 python shell 中运行 follow 时,它给了我 poll 对象而不是问题。
>>> Poll.objects.all()
[<Poll: Poll object>]

最佳答案

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

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

关于python - django教程: unexpected indent error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25214870/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com