在应用程序目录中的命令行上键入后:
python manage.py runserver
我收到此错误:
Traceback (most recent call last):
File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "C:\Users\Paulo\Coding\Python\lib\site-packages\django\core\management\__init__.py", line 371, in execute_from_command_line
utility.execute()
File "C:\Users\Paulo\Coding\Python\lib\site-packages\django\core\management\__init__.py", line 347, in execute
django.setup()
File "C:\Users\Paulo\Coding\Python\lib\site-packages\django\__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\Users\Paulo\Coding\Python\lib\site-packages\django\apps\registry.py", line 112, in populate
app_config.import_models()
File "C:\Users\Paulo\Coding\Python\lib\site-packages\django\apps\config.py", line 198, in import_models
self.models_module = import_module(models_module_name)
File "C:\Users\Paulo\Coding\Python\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 678, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "C:\Users\Paulo\Coding\Django projects\Project 1\freezer\models.py", line 5, in <module>
class Item(models.Model):
File "C:\Users\Paulo\Coding\Django projects\Project 1\freezer\models.py", line 31, in Item
added_date = models.DateField("date added", default=datetime.date.today)
NameError: name 'datetime' is not defined
这个标题类似的question并没有帮助我。我有
from datetime import date
在我的模型中。
我正在运行python 3.6.5和Django 2.0.4。我前不久将该应用程序在线上,它仍在工作,并且models.py相同。我对自己做错了事感到困惑。可能与最近必须重置Windows 10有关吗?它删除了该过程中的python安装(在我的程序文件目录中)和许多其他内容。
它留下了我的一个python安装。从命令行打开它,导入datetime,并使用该函数返回今天的日期。
这是我的模型。py:
from django.db import models
from django.utils import timezone
from datetime import date
class Item(models.Model):
FREEZER_DRAWERS = (
(1, 'Kitchen: Top tray'),
(2, 'Kitchen: Middle drawer'),
(3, 'Kitchen: Bottom drawer'),
)
TYPES = (
(1, 'Raw meat'),
(2, 'Fruit and veg'),
(3, 'Meal'),
)
author = models.ForeignKey('auth.User', on_delete=models.CASCADE)
title = models.CharField("food", max_length=100)
item_type = models.IntegerField(choices = TYPES, default = 1)
added_date = models.DateField("date added", default=datetime.date.today)
where = models.IntegerField(choices = FREEZER_DRAWERS, default = 1)
expires_date = models.DateField(default = datetime.date.today)
on_shopping_list = models.BooleanField()
def __str__(self):
return self.title
谢谢!这是我在这些论坛上找不到的年龄以来的第一个问题!
答案 0 :(得分:2)
问题是您没有导入datetime
模块(或者至少没有以限定的方式):您导入了date
属性。
date
类您可以仅用datetime.date
替换date
(这是您唯一需要的元素):
from django.db import models
from django.utils import timezone
from datetime import date
class Item(models.Model):
FREEZER_DRAWERS = (
(1, 'Kitchen: Top tray'),
(2, 'Kitchen: Middle drawer'),
(3, 'Kitchen: Bottom drawer'),
)
TYPES = (
(1, 'Raw meat'),
(2, 'Fruit and veg'),
(3, 'Meal'),
)
author = models.ForeignKey('auth.User', on_delete=models.CASCADE)
title = models.CharField("food", max_length=100)
item_type = models.IntegerField(choices = TYPES, default = 1)
added_date = models.DateField("date added", default=date.today)
where = models.IntegerField(choices = FREEZER_DRAWERS, default = 1)
expires_date = models.DateField(default = date.today)
on_shopping_list = models.BooleanField()
def __str__(self):
return self.title
或者,您可以导入模块,然后使用datetime.date
:
from django.db import models
from django.utils import timezone
import datetime
class Item(models.Model):
FREEZER_DRAWERS = (
(1, 'Kitchen: Top tray'),
(2, 'Kitchen: Middle drawer'),
(3, 'Kitchen: Bottom drawer'),
)
TYPES = (
(1, 'Raw meat'),
(2, 'Fruit and veg'),
(3, 'Meal'),
)
author = models.ForeignKey('auth.User', on_delete=models.CASCADE)
title = models.CharField("food", max_length=100)
item_type = models.IntegerField(choices = TYPES, default = 1)
added_date = models.DateField("date added", default=datetime.date.today)
where = models.IntegerField(choices = FREEZER_DRAWERS, default = 1)
expires_date = models.DateField(default = datetime.date.today)
on_shopping_list = models.BooleanField()
def __str__(self):
return self.title
auto_now_add
中的DateField
不管我们如何导入它,事实上Django已经支持这种默认值:auto_now_add=True
[Django-doc]:这将添加一个等于today
的默认值。此外,它将使字段blank=True
和editable=False
阻止其以表单等形式出现。因此, 不是完全等效的,尽管很可能是意思是:
# Note: makes the fields blank=True, and editable=False as well.
from django.db import models
from django.utils import timezone
class Item(models.Model):
FREEZER_DRAWERS = (
(1, 'Kitchen: Top tray'),
(2, 'Kitchen: Middle drawer'),
(3, 'Kitchen: Bottom drawer'),
)
TYPES = (
(1, 'Raw meat'),
(2, 'Fruit and veg'),
(3, 'Meal'),
)
author = models.ForeignKey('auth.User', on_delete=models.CASCADE)
title = models.CharField("food", max_length=100)
item_type = models.IntegerField(choices = TYPES, default = 1)
added_date = models.DateField("date added", auto_now_add=True)
where = models.IntegerField(choices = FREEZER_DRAWERS, default = 1)
expires_date = models.DateField(auto_now_add=True)
on_shopping_list = models.BooleanField()
def __str__(self):
return self.title