我正在使用python和django进行测试项目(出于学习目的)。在某个时候,我必须制作一个名为products的django应用程序,然后在“ products”应用程序目录中的models.py文件中输入以下代码:
class Products (models.Model)
title = models.TextField()
description = models.TextField()
price = models.TextField()
,然后在我参加设置迁移时将新应用添加到设置文件中,如图所示:
> python manage.py makemigrations
我得到的是这个错误:
Traceback (most recent call last):
File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "C:\django\lib\site-packages\django\core\management\__init__.py", line 371, in execute_from_command_line
utility.execute()
File "C:\django\lib\site-packages\django\core\management\__init__.py", line 347, in execute
django.setup()
File "C:\django\lib\site-packages\django\__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\django\lib\site-packages\django\apps\registry.py", line 112, in populate
app_config.import_models()
File "C:\django\lib\site-packages\django\apps\config.py", line 198, in import_models
self.models_module = import_module(models_module_name)
File "C:\django\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 724, in exec_module
File "<frozen importlib._bootstrap_external>", line 860, in get_code
File "<frozen importlib._bootstrap_external>", line 791, in source_to_code
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "C:\django\mystore\products\models.py", line 5
class Products(models.Model)
^
SyntaxError: invalid syntax
"
,然后每当我尝试使用manage.py文件时,都会发生相同的错误。我不知道我做错了什么,请帮助我解决此问题。
答案 0 :(得分:1)
在类声明后您缺少一个冒号。
class Products(models.Model):
此外,我会提醒您进行格式化。您的字段名称应在左侧对齐。在Python中,不对齐的两行被视为不同的代码块。