gpt4 book ai didi

Python Django 名称错误 : name 'datetime' is not defined

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

在应用程序目录中的命令行中键入后:

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

在我的 models.py 中。

我正在运行 python 3.6.5 和 Django 2.0.4。我刚才把这个应用程序放到网上,它还在工作,models.py 是相同的。我对自己做错了什么感到困惑。是否与最近必须重置我的 Windows 10 有关?它删除了进程中的 python 安装(在我的程序文件目录中)和许多其他东西。
它单独留下了我的一个 python 安装。我从命令行打开它并导入日期时间并使用该函数返回今天的日期。

这是我的模型.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

谢谢!这是多年来我在这些论坛上无法回答的第一个问题!

最佳答案

问题是您没有导入 datetime 模块(或者至少没有以合格的方式导入):您导入了 date 属性。

使用日期

您可以将 datetime.date 替换为 date(这是您唯一需要的元素):

from django.db import models
from django.utils import timezone
from datetime import <b>date</b>

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=<b>date</b>.today)
where = models.IntegerField(choices = FREEZER_DRAWERS, default = 1)
expires_date = models.DateField(default = <b>date</b>.today)
on_shopping_list = models.BooleanField()

def __str__(self):
return self.title

导入模块

或者,您可以导入模块,然后使用 datetime.date:

from django.db import models
from django.utils import timezone
<b>import datetime</b>

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=<b>datetime.date</b>.today)
where = models.IntegerField(choices = FREEZER_DRAWERS, default = 1)
expires_date = models.DateField(default = <b>datetime.date</b>.today)
on_shopping_list = models.BooleanField()

def __str__(self):
return self.title

使用 DateFieldauto_now_add

无论我们如何导入它,事实上 Django 已经支持这样的默认值:使用 auto_now_add=True [Django-doc] : 这将添加一个等于 today 的默认值。此外,它将使字段 blank=Trueeditable=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"<b>, auto_now_add=True</b>)
where = models.IntegerField(choices = FREEZER_DRAWERS, default = 1)
expires_date = models.DateField(<b>auto_now_add=True</b>)
on_shopping_list = models.BooleanField()

def __str__(self):
return self.title

关于Python Django 名称错误 : name 'datetime' is not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51987827/

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