新人求问,正在学习python做博客,
from django.db import models Class Topic(models.Model): """用户学习的主题""" text = models.CharField(max_length=200) date_added = models.DateTimeField(auto_now_add=True) def __srt__(self): """返回模型的字符串表示""" return self.text
在models.py中运行上面的代码提示出错:
File "E:/Python/learning_log/env1/Scripts/learning_logs/models.py", line 3 Class Topic(models.Model): ^ SyntaxError: invalid syntax
请问是什么原因?
另外在命令行执行python manage.py makemigrations也报错:
E:\Python\learning_log\env1\Scripts>activate (env1) E:\Python\learning_log\env1\Scripts>python manage.py makemigrations Traceback (most recent call last): File "manage.py", line 15, in <module> execute_from_command_line(sys.argv) File "E:\Python\learning_log\env1\lib\site-packages\django\core\management\__init__.py", line 371, in execute_from_command_line utility.execute() File "E:\Python\learning_log\env1\lib\site-packages\django\core\management\__init__.py", line 347, in execute django.setup() File "E:\Python\learning_log\env1\lib\site-packages\django\__init__.py", line 24, in setup apps.populate(settings.INSTALLED_APPS) File "E:\Python\learning_log\env1\lib\site-packages\django\apps\registry.py", line 112, in populate app_config.import_models() File "E:\Python\learning_log\env1\lib\site-packages\django\apps\config.py", line 198, in import_models self.models_module = import_module(models_module_name) File "E:\Python\learning_log\env1\lib\importlib\__init__.py", line 126, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "<frozen importlib._bootstrap>", line 994, in _gcd_import File "<frozen importlib._bootstrap>", line 971, in _find_and_load File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 665, in _load_unlocked File "<frozen importlib._bootstrap_external>", line 674, in exec_module File "<frozen importlib._bootstrap_external>", line 781, in get_code File "<frozen importlib._bootstrap_external>", line 741, in source_to_code File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed File "E:\Python\learning_log\env1\Scripts\learning_logs\models.py", line 3 Class Topic(models.Model): ^ SyntaxError: invalid syntax
很明显,text = models.CharField(max_length=200)定义的顶格了,注意缩进,还有最后的return self.text,也在def __srt__(self)里
1 class Topic(models.Model): 2 """用户学习的主题""" 3 text = models.CharField(max_length=200) 4 date_added = models.DateTimeField(auto_now_add=True) 5 6 def __srt__(self): 7 """返回模型的字符串表示""" 8 return self.text
格式没有问题 只不过粘贴过来时候格式乱了。。
@Twinko: class关键字首字母小写
@北方姆Q: 改成小写之后还是报错:
E:\Python\learning_log\env1\Scripts\python.exe E:/Python/learning_log/env1/Scripts/learning_logs/models.py Traceback (most recent call last): File "E:/Python/learning_log/env1/Scripts/learning_logs/models.py", line 3, in <module> class Topic(models.Model): File "E:\Python\learning_log\env1\lib\site-packages\django\db\models\base.py", line 100, in __new__ app_config = apps.get_containing_app_config(module) File "E:\Python\learning_log\env1\lib\site-packages\django\apps\registry.py", line 244, in get_containing_app_config self.check_apps_ready() File "E:\Python\learning_log\env1\lib\site-packages\django\apps\registry.py", line 127, in check_apps_ready raise AppRegistryNotReady("Apps aren't loaded yet.") django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
@Twinko: 嗯,这个你要去你的setting里的INSTALLED_APPS的大列表内添加你的这个app的名称
@北方姆Q: 已经添加了'learning_logs', 还是报错。
@Twinko: python manage.py makemigrations
python manage.py migrate
这两个现在有报错嘛,还是已经通过了?
@北方姆Q: 已经通过啦