gpt4 book ai didi

python - 为什么此命令 python manage.py shell 出现错误?

转载 作者:太空宇宙 更新时间:2023-11-03 18:42:21 25 4
gpt4 key购买 nike

我使用Django 1.6。我像这样编辑我的 models.py:

(我点击此链接:tutorial01)

from django.db import models
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 shell

我的错误是:

Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line
399, in execute_from_command_line
utility.execute()
File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line
392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Python27\lib\site-packages\django\core\management\base.py", line 242,
in run_from_argv
self.execute(*args, **options.__dict__)
File "C:\Python27\lib\site-packages\django\core\management\base.py", line 285,
in execute
output = self.handle(*args, **options)
File "C:\Python27\lib\site-packages\django\core\management\base.py", line 415,
in handle
return self.handle_noargs(**options)
File "C:\Python27\lib\site-packages\django\core\management\commands\shell.py",
line 70, in handle_noargs
get_models()
File "C:\Python27\lib\site-packages\django\db\models\loading.py", line 232, in
get_models
self._populate()
File "C:\Python27\lib\site-packages\django\db\models\loading.py", line 75, in
_populate
self.load_app(app_name, True)
File "C:\Python27\lib\site-packages\django\db\models\loading.py", line 99, in
load_app
models = import_module('%s.models' % app_name)
File "C:\Python27\lib\site-packages\django\utils\importlib.py", line 40, in im
port_module
__import__(name)
File "G:\Programing\Django\Project\mysite\polls\models.py", line 7
def __str__(self):
^
IndentationError: unexpected indent

我认为这一行的内容是 def __str__(self): return self.question 错了吗?

最佳答案

您的 def __str__ 需要与类主体的其余部分处于相同的缩进级别。确保类体和def的内容都缩进4个空格。您混合了制表符和空格缩进,这使解析器感到困惑。避免使用制表符进行缩进 - 每个级别只需坚持 4 个空格即可。当您按 Tab 键时,您的编辑器应该可以选择插入 4 个空格。

关于python - 为什么此命令 python manage.py shell 出现错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20272785/

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