ModuleNotFoundError: 没有名为 "路由器 "的模块

嗨,我得到这个错误,请帮助我.我对这个领域的新,我得到这个错误。

设置.py

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'rest_framework',

    'first',
]

我的views.py

from django.shortcuts import render
from rest_framework.viewsets import ModelViewSet
from .serializers import MovieSerializer,RatingSerializers
from .models import Movie,Rating
# Create your views here.
class MovieViewSet(ModelViewSet):
    queryset=Movie.objects.all()
    serializer_class=(MovieSerializer,)
class RatingViewSet(ModelViewSet):
    queryset=Rating.objects.all()
    serializer_class=(RatingSerializers,)

我的主尿素.py是

"""rest_project URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/3.0/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path,include

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

和serializers.py

from rest_framework import serializers
from .models import Movie,Rating
class MovieSerializer(serializers.ModelSerializer):
    class Meta:
        model= Movie
        fields=['title','description']

class RatingSerializers(serializers.ModelSerializer):
    class meta:
        model=Rating
        fields='__all__'

模型.py是

from django.db import models
from django.contrib.auth.models import User
from django.core.validators import MinValueValidator,MaxValueValidator
# Create your models here.
class Movie(models.Model):
    title=models.CharField(max_length=20)
    description =models.TextField(max_length=500)

    def __str__(self):
        return self.title

class Rating(models.Model):
    movie=models.ForeignKey(Movie,on_delete=models.CASCADE)
    user=models.ForeignKey(User,on_delete=models.CASCADE)
    rating=models.PositiveIntegerField(validators=[MinValueValidator(1),MaxValueValidator(5)])
    class Meta:
        unique_together=(('user','movie'))
        index_together=(('user','movie'))

我的 urls.py (应用程序)

from django.urls import path
from django.conf.urls import include
from rest_framework import routers
from .views import ModelViewSet,RatingViewSet

router=routers.DefaultRouter()

router.register('movies',ModelViewSet,basename='Movie')
router.register('rating',RatingViewSet,basename='Rating')

app_name='first'

urlpatterns = [
    path('',include('router.urls')),
]

我正在按照视频教程

并得到该错误

    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 965, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'router'

请帮我忽略这段文字是为了避免 “看起来像帖子里大部分是代码,请加点东西”

解决方案:

urlpatterns = [
    path('',include('router.urls')),
]

应该是。

urlpatterns = router.urls

包含表达式去寻找一个路由器模块。

给TA打赏
共{{data.count}}人
人已打赏
未分类

全屏被打破 - 库存工具

2022-9-8 5:41:40

未分类

将多行数据查询为一列

2022-9-8 5:41:42

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索