首页 文章

使用virtualenv在django中创建了新项目并且运行正常,但是当我尝试使用'startapp'创建新应用程序并再次运行项目时

提问于
浏览
0

使用virtualenv在django中创建了新项目,并且运行良好,但当我尝试使用'startapp'创建新应用程序并再次按命令运行项目时:python3.6 advertisement / manage.py runserver localhost: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'

注意:我已在settings.py安装的应用中添加了应用 .

请帮我重新解决这个问题 .

2 回答

  • 1

    只有在创建应用程序并创建URL时才会出现这种类型的错误,但是您不在urls.py文件中引用的views.py文件中创建函数 . 解决方案如下:

    • 在settings.py文件中声明了您的应用名称

    • 在main urls.py文件中声明了您的url

    • 在您的app urls.py文件中声明了您的网址

    • 在您的app urls.py文件中引用的views.py文件中声明了您的函数现在,您可以通过两种方法运行服务器 .

    python manage.py runserver python3.6 manage.py runserver

  • 0

    添加一些INSTALLED_APPS的代码 . 你只是说django添加了一个应用程序“core”,它位于mysite文件夹中,而django无法找到它 .

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

    2)在mysite中创建一个app核心 . 并创建一个应用程序意味着它应该有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
    

相关问题