Python 使用virtualenv在django中创建了新项目,运行良好,但当我尝试使用';startapp&x27;再次运行项目

Python 使用virtualenv在django中创建了新项目,运行良好,但当我尝试使用';startapp&x27;再次运行项目,python,django,virtualenv,Python,Django,Virtualenv,使用virtualenv在django中创建了新项目,运行良好,但当我尝试使用“startapp”创建新应用程序并再次通过命令运行project时: python3.6播发/manage.py运行服务器本地主机:8000 then throw error : Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x7f57db4171e0> Traceba

使用virtualenv在django中创建了新项目,运行良好,但当我尝试使用“startapp”创建新应用程序并再次通过命令运行project时: python3.6播发/manage.py运行服务器本地主机: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'
然后抛出错误:
由启动的线程中存在未处理的异常
回溯(最近一次呼叫最后一次):
包装器中的文件“/home/ravinder/advision/venv/lib/python3.6/site packages/django/utils/autoreload.py”,第225行
fn(*args,**kwargs)
文件“/home/ravinder/advision/venv/lib/python3.6/site packages/django/core/management/commands/runserver.py”,第112行,在内部运行
autoreload.raise\u last\u异常()
文件“/home/ravinder/advision/venv/lib/python3.6/site packages/django/utils/autoreload.py”,第248行,在raise\u last\u异常中
raise_异常[1]
文件“/home/ravinder/advision/venv/lib/python3.6/site packages/django/core/management/__init__.py”,执行中第327行
自动加载检查错误(django.setup)()
包装器中的文件“/home/ravinder/advision/venv/lib/python3.6/site packages/django/utils/autoreload.py”,第225行
fn(*args,**kwargs)
文件“/home/ravinder/advision/venv/lib/python3.6/site packages/django/_init__.py”,第24行,在安装程序中
应用程序。填充(设置。已安装的应用程序)
文件“/home/ravinder/advision/venv/lib/python3.6/site packages/django/apps/registry.py”,第89行,在“填充”中
app_config=AppConfig.create(条目)
文件“/home/ravinder/advision/venv/lib/python3.6/site packages/django/apps/config.py”,第90行,在create中
模块=导入模块(条目)
文件“/usr/lib/python3.6/importlib/_init_uuu.py”,第126行,在导入模块中
return _bootstrap._gcd_import(名称[级别:],包,级别)
文件“”,第994行,在_gcd_导入中
文件“”,第971行,在_find_和_load中
文件“”,第953行,在“查找”和“加载”中解锁
ModuleNotFoundError:没有名为“core”的模块
注意:我已经在setting.py安装的应用程序中添加了应用程序


请帮我解决这个问题。

添加一些已安装应用程序的代码。你刚才说django要添加一个应用程序“core”,它位于mysite文件夹中,django无法找到它

1) 删除mysite.core,然后运行migrate

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

或者你在已安装的应用程序中提到了你的应用程序,如下所示

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
那么你的目录应该是这样的

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

只有当您创建应用程序并生成URL,但未在views.py文件中创建函数(在url.py文件中引用)时,才会发生此类错误。解决方案如下:

  • 在settings.py文件中声明了您的应用程序名称
  • 在主url.py文件中声明了您的url
  • 在app url.py文件中声明了您的url
  • 在views.py文件中声明函数,并在app url.py文件中引用该文件 现在,您可以通过两种方法运行服务器
  • python manage.py运行服务器

    python3.6 manage.py运行服务器