运行“python manage.py startapp blog”时显示以下错误

时间:2017-12-14 15:59:52

标签: django python-3.x django-models migration database-migration

我正在学习djangogirls教程,当我在myenv(虚拟环境)中运行python manage.py startapp blog时,它会显示以下错误,并且根本无法运行任何内容

错误是

Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "c:\Users\Divyesh\djangogirls\myenv\lib\site-packages\django    \core\management\__init__.py", line 371, in execute_from_command_line
    utility.execute()
  File "c:\Users\Divyesh\djangogirls\myenv\lib\site-packages\django\core\management\__init__.py", line 347, in execute
    django.setup()
  File "c:\Users\Divyesh\djangogirls\myenv\lib\site-packages\django\__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "c:\Users\Divyesh\djangogirls\myenv\lib\site-packages\django\apps\registry.py", line 112, in populate
    app_config.import_models()
  File "c:\Users\Divyesh\djangogirls\myenv\lib\site-packages\django\apps\config.py", line 198, in import_models
    self.models_module = import_module(models_module_name)
  File "C:\Program Files (x86)\Python36-32\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\Divyesh\djangogirls\dpblog\models.py", line 2, in <module>
    from utils import timezone
ModuleNotFoundError: No module named 'utils'

在跑步之后也是如此     python manage.py makemigrations blog

它显示几乎相同的错误 我不知道如何处理这个请帮忙。第一次学习Django。

4 个答案:

答案 0 :(得分:1)

根据使用时区的个人经验,以及基于Django Docs,您应该使用:

from django.utils import timezone

而不是from utils import timezone

希望这有帮助!

答案 1 :(得分:1)

我遇到了同样的问题。我不知道您是否应该这样做,但是我转到第14行的文件manage.py并注释了from exc

所以我在manage.py上的第14行看起来像这样:

        )  # from exc

答案 2 :(得分:0)

我遇到了同样的错误,并且也得到了解决方案。 出现错误是因为您尚未激活Django脚本。

在django教程文件夹中打开cmd,然后键入:Scripts\activate。 例如E:\django tutorial\blogcourse>Scripts\activate

完成此操作后,在命令行上将如下所示: (blogcourse) E:\django tutorial\blogcourse\src>

然后您可以输入python manage.py startapp personel 例如: (blogcourse) E:\django tutorial\blogcourse\src>python manage.py startapp personel

您已经完成了:)

this is how it is done on my pc

答案 3 :(得分:0)

试试这个...

  1. 打开 settings.py 文件
  2. 删除 INSTALL_APPS 下的“utils”和博客(如果有)
  3. INSTALLED_APPS=['django.contrib.admin','django.contrib.auth','django.contrib.contenttypes','django.contrib.sessions','django.contrib.messages','django.contrib.staticfiles','blog','utils']
  4. 保存文件并尝试运行 python manage.py startapp博客

这个解决方案对我有用。