首页 > 解决方案 > ImportError:无法从“rest_framework”导入名称“路由器”

问题描述

我刚开始学习 Django REST 框架。我正在尝试完成官方文档中的快速入门。我收到此错误:

Exception in thread django-main-thread:
Traceback (most recent call last):
  File "F:\Python\lib\threading.py", line 932, in _bootstrap_inner
    self.run()
  File "F:\Python\lib\threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "F:\Python\lib\site-packages\django\utils\autoreload.py", line 53, in
wrapper
    fn(*args, **kwargs)
  File "F:\Python\lib\site-packages\django\core\management\commands\runserver
.py", line 118, in inner_run
    self.check(display_num_errors=True)
  File "F:\Python\lib\site-packages\django\core\management\base.py", line 392
, in check
    all_issues = checks.run_checks(
  File "F:\Python\lib\site-packages\django\core\checks\registry.py", line 70,
 in run_checks
    new_errors = check(app_configs=app_configs, databases=databases)
  File "F:\Python\lib\site-packages\django\core\checks\urls.py", line 13, in
check_url_config
    return check_resolver(resolver)
  File "F:\Python\lib\site-packages\django\core\checks\urls.py", line 23, in
check_resolver
    return check_method()
  File "F:\Python\lib\site-packages\django\urls\resolvers.py", line 408, in c
heck
    for pattern in self.url_patterns:
  File "F:\Python\lib\site-packages\django\utils\functional.py", line 48, in
__get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "F:\Python\lib\site-packages\django\urls\resolvers.py", line 589, in u
rl_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_modul
e)
  File "F:\Python\lib\site-packages\django\utils\functional.py", line 48, in
__get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "F:\Python\lib\site-packages\django\urls\resolvers.py", line 582, in u
rlconf_module
    return import_module(self.urlconf_name)
  File "F:\Python\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_remove
d
  File "F:\Programming\Python\rest_framework\rest_framework\urls.py", line 18
, in <module>
    from rest_framework import routers
ImportError: cannot import name 'routers' from 'rest_framework' (F:\Programmi
ng\Python\rest_framework\rest_framework\__init__.py)

网址.py

from django.contrib import admin
from django.urls import path, include
from rest_framework import routers
from quickstart import views

router = routers.DefaultRouter()
router.register(r'users', views.UserViewSet)
router.register(r'groups', views.GroupViewSet)



urlpatterns = [
    path('admin/', admin.site.urls),
    path('api-auth/', include('rest_framework.urls', namespace='rest_framework'))

]

视图.py

from django.shortcuts import render
from django.contrib.auth.models import User, Group
from rest_framework import viewsets, permissions
from quickstart.serializers import UserSerializer, GroupSerializer


class UserViewSet(viewsets.ModelViewSet):
    """API endpoint that allows users to be viewed or edited."""
    queryset = User.objects.all().order_by('-date_joined')
    serializer_class = UserSerializer
    permission_classes = [permissions.IsAuthenticated]

class GroupViewSet(viewsets.ModelViewSet):
    """API endpoint that allows groups to be viewed or edited."""
    queryset = Group.objects.all()
    serializer_class = GroupSerializer
    permission_classes = [permissions.IsAuthenticated]

当然,我已经安装了 Rest Framework 和 Django。如果有帮助,我正在使用 Python 3.7.9。我已将其余框架添加到我安装的应用程序中:.

INSTALLED_APPS = [
        'django.contrib.admin',
        'rest_framework',
        'django.contrib.auth',
        'quickstart.apps.QuickstartConfig',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
    ]

我已经检查了有关路由器等的官方文档,它们以与我相同的方式导入路由器,但由于某些奇怪的原因我的不起作用。可能是什么问题?

谢谢您的帮助!:)

标签: pythondjangodjango-rest-framework

解决方案


推荐阅读