ModuleNotFoundError:没有名为“To_do.todos”的模块- DJANGO

5t7ly7z5  于 2023-05-19  发布在  Go
关注(0)|答案(4)|浏览(144)

所以我刚开始学习Django,我是从这个YouTube视频中学习的:https://www.youtube.com/watch?v=Nnoxz9JGdLU
下面是我的目录Map:

To_do
+To_do
 +_pycache_
 +_init_.py
 +asgi.py
 +settings.py
 +urls.py
 +wsgi.py 
+todos
 +_pycache_
 +migrations
 +_init_.py
 +admin.py
 +apps.py
 +models.py
 +tests.py
 +urls.py
 +views.py
+db.sqlite3
+manage.py

To_do/To_do/ www.example.com的代码urls.py

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('todos/', include('todos.urls'))
]

代码:To_do/todos/urls.py

from django.urls import path
from . import views
urlpatterns =[
    path('list/',views.list_todo_items)
]

代码:To_do/todos/view.py

from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.

def list_todo_items(request):
    return HttpResponse('from list_todo_items')

我的问题:

在使用上面的代码和重定向后,很明显我在某个地方搞砸了,就像在项目目录中的“main”urls.py文件中一样,当我运行我的服务器时,我得到了错误:

$ python3 manage.py runserver
Watching for file changes with StatReloader
Performing system checks...

Exception in thread django-main-thread:
Traceback (most recent call last):
  File "/usr/lib/python3.6/threading.py", line 916, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.6/threading.py", line 864, in run
    self._target(*self._args, **self._kwargs)
  File "/home/smith/.local/lib/python3.6/site-packages/django/utils/autoreload.py", line 53, in wrapper
    fn(*args, **kwargs)
  File "/home/smith/.local/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 117, in inner_run
    self.check(display_num_errors=True)
  File "/home/smith/.local/lib/python3.6/site-packages/django/core/management/base.py", line 395, in check
    include_deployment_checks=include_deployment_checks,
  File "/home/smith/.local/lib/python3.6/site-packages/django/core/management/base.py", line 382, in _run_checks
    return checks.run_checks(**kwargs)
  File "/home/smith/.local/lib/python3.6/site-packages/django/core/checks/registry.py", line 72, in run_checks
    new_errors = check(app_configs=app_configs)
  File "/home/smith/.local/lib/python3.6/site-packages/django/core/checks/urls.py", line 40, in check_url_namespaces_unique
    all_namespaces = _load_all_namespaces(resolver)
  File "/home/smith/.local/lib/python3.6/site-packages/django/core/checks/urls.py", line 57, in _load_all_namespaces
    url_patterns = getattr(resolver, 'url_patterns', [])
  File "/home/smith/.local/lib/python3.6/site-packages/django/utils/functional.py", line 48, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/smith/.local/lib/python3.6/site-packages/django/urls/resolvers.py", line 588, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/home/smith/.local/lib/python3.6/site-packages/django/utils/functional.py", line 48, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/smith/.local/lib/python3.6/site-packages/django/urls/resolvers.py", line 581, in urlconf_module
    return import_module(self.urlconf_name)
  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 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 "/home/smith/To_do/To_do/urls.py", line 21, in <module>
    path('todos/', include('To_do.todos.urls'))
  File "/home/smith/.local/lib/python3.6/site-packages/django/urls/conf.py", line 34, in include
    urlconf_module = import_module(urlconf_module)
  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 941, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  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 'To_do.todos'

很明显,在urls.py“To_do”文件夹中的“urls.py”中提到的“todos”文件夹下找不到“www.example.com”文件。

n8ghc7c1

n8ghc7c11#

根据错误path('todos/', include('To_do.todos.urls'))应更改为:path('todos/', include('todo/',include('to_do.urls')) #to_do作为应用程序的名称。

ny6fqffe

ny6fqffe2#

根据错误path('todos/', include('To_do.todos.urls'))应更改为

path('todos/', include('todos.urls'))
xdnvmnnf

xdnvmnnf3#

检查您是否在www.example.com的已安装应用列表中添加了todos应用settings.py

INSTALLED_APPS = [
    ...
    'todos.apps.TodosConfig'
]

停止服务器ctrl + c然后再次运行服务器python manage.py runserver

drnojrws

drnojrws4#

答案很简单。我得到这个错误,因为我有我的应用程序以外的todo文件夹。django项目的项目结构应该是这样的:

todo
   todo
   todo_app
   db.sqllite3
   manage.py

我做了这些修改,错误就消失了

相关问题