Django使用rest_framework插件,但是无法运行,是什么原因?

如题所述报错如下:

Exception in thread django-main-thread:
Traceback (most recent call last):
  File "/home/*****/.local/lib/python3.12/site-packages/django/utils/module_loading.py", line 25, in import_string
    module_path, class_name = dotted_path.rsplit(".", 1)
    ^^^^^^^^^^^^^^^^^^^^^^^
ValueError: not enough values to unpack (expected 2, got 1)

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/****/.local/lib/python3.12/site-packages/django/core/servers/basehttp.py", line 48, in get_internal_wsgi_application
    return import_string(app_path)
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/****/.local/lib/python3.12/site-packages/django/utils/module_loading.py", line 30, in import_string
    return cached_import(module_path, class_name)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/****/.local/lib/python3.12/site-packages/django/utils/module_loading.py", line 15, in cached_import
    module = import_module(module_path)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/importlib/__init__.py", line 90, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1387, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 935, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 995, in exec_module
  File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed
  File "/home/***/Documents/soft_souces/music/djangoMusicProject/wsgi.py", line 16, in <module>
    application = get_wsgi_application()
                  ^^^^^^^^^^^^^^^^^^^^^^
  File "/home/……………………/.local/lib/python3.12/site-packages/django/core/wsgi.py", line 13, in get_wsgi_application
    return WSGIHandler()
           ^^^^^^^^^^^^^
  File "/home/****/.local/lib/python3.12/site-packages/django/core/handlers/wsgi.py", line 118, in __init__
    self.load_middleware()
  File "/home/***/.local/lib/python3.12/site-packages/django/core/handlers/base.py", line 40, in load_middleware
    middleware = import_string(middleware_path)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/****/.local/lib/python3.12/site-packages/django/utils/module_loading.py", line 27, in import_string
    raise ImportError("%s doesn't look like a module path" % dotted_path) from err
ImportError: rest_framework doesn't look like a module path

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/lib/python3.12/threading.py", line 1073, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.12/threading.py", line 1010, in run
    self._target(*self._args, **self._kwargs)
  File "/home/****/.local/lib/python3.12/site-packages/django/utils/autoreload.py", line 64, in wrapper
    fn(*args, **kwargs)
  File "/home/****/.local/lib/python3.12/site-packages/django/core/management/commands/runserver.py", line 143, in inner_run
    handler = self.get_handler(*args, **options)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/****/.local/lib/python3.12/site-packages/django/contrib/staticfiles/management/commands/runserver.py", line 31, in get_handler
    handler = super().get_handler(*args, **options)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/****/.local/lib/python3.12/site-packages/django/core/management/commands/runserver.py", line 79, in get_handler
    return get_internal_wsgi_application()
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/***/.local/lib/python3.12/site-packages/django/core/servers/basehttp.py", line 50, in get_internal_wsgi_application
    raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: WSGI application 'djangoMusicProject.wsgi.application' could not be loaded; Error importing module.

这是什么原因,是我没有配置好吗?
我的配置如下:

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'rest_framework',
]

REST_FRAMEWORK = {
    'DEFAULT_PERMISSION_CLASSES': [
        'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly',
    ]
}

这里我最想不明白的是为什么会报:ImportError: rest_framework doesn't look like a module path
这个错误!

阅读 428
1 个回答

rest_framework 不是一个中间件(middleware),它应该放在 INSTALLED_APPS 中而不是 MIDDLEWARE 中。

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    # 添加 rest_framework 到这里
    'rest_framework',
    # 你的其他应用...
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    # 删除这里的 rest_framework
]

# REST_FRAMEWORK 配置保持不变
REST_FRAMEWORK = {
    'DEFAULT_PERMISSION_CLASSES': [
        'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly',
    ]
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏