使用virtualenv在Django中创建了新项目,并且运行良好,但是当我尝试使用“ startapp”创建新应用并再次运行项目时

时间:2018-12-09 03:51:18

标签: python django virtualenv

使用virtualenv在Django中创建了新项目,并且运行良好,但是当我尝试使用“ startapp”创建新应用并再次通过命令运行项目时: python3.6 advertising / manage.py runserver本地主机:8000

then throw error : 
Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x7f57db4171e0>
Traceback (most recent call last):
  File "/home/ravinder/advertisement/venv/lib/python3.6/site-packages/django/utils/autoreload.py", line 225, in wrapper
    fn(*args, **kwargs)
  File "/home/ravinder/advertisement/venv/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 112, in inner_run
    autoreload.raise_last_exception()
  File "/home/ravinder/advertisement/venv/lib/python3.6/site-packages/django/utils/autoreload.py", line 248, in raise_last_exception
    raise _exception[1]
  File "/home/ravinder/advertisement/venv/lib/python3.6/site-packages/django/core/management/__init__.py", line 327, in execute
    autoreload.check_errors(django.setup)()
  File "/home/ravinder/advertisement/venv/lib/python3.6/site-packages/django/utils/autoreload.py", line 225, in wrapper
    fn(*args, **kwargs)
  File "/home/ravinder/advertisement/venv/lib/python3.6/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/home/ravinder/advertisement/venv/lib/python3.6/site-packages/django/apps/registry.py", line 89, in populate
    app_config = AppConfig.create(entry)
  File "/home/ravinder/advertisement/venv/lib/python3.6/site-packages/django/apps/config.py", line 90, in create
    module = import_module(entry)
  File "/usr/lib/python3.6/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 953, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'core'

注意:我已经在安装了setting.py的应用中添加了该应用。

请帮助我解决这个问题。

2 个答案:

答案 0 :(得分:1)

添加一些INSTALLED_APPS的代码。您只是说django在mysite文件夹中添加了一个应用程序“核心”,而django无法找到它。

1)删除mysite.core,然后运行迁移。

2)在mysite中创建一个应用核心。并创建一个应用意味着它应该具有app.py文件。它包含

或者您在这样的INSTALLED_APPS中提到了您的应用

INSTALLED_APPS = [ 
       'django.contrib.admin', 
       'django.contrib.auth', 
       'django.contrib.contenttypes', 
       'django.contrib.sessions', 
       'django.contrib.messages', 
       'django.contrib.staticfiles', 

       'mysite.core', 
] 

然后您的目录应该是这样

mysite/
 ├── core
 │     ├── __init__.py
 │     └── views.py
 └── __init__.py

答案 1 :(得分:0)

仅当您创建应用并创建网址但没有在urls.py文件中引用的views.py文件中创建函数时,才会发生此类错误。解决方案如下:

  1. 在settings.py文件中声明了您的应用名称
  2. 在主urls.py文件中声明了您的网址
  3. 在应用的urls.py文件中声明了您的网址
  4. 在views.py文件中声明了您的功能,您在应用程序urls.py文件中进行了引用 现在,您可以通过两种方法来运行服务器。
  

python manage.py runserver

     

python3.6 manage.py运行服务器