我试图弄清楚django rest框架是如何工作的,并编写一些代码。但是我得到
django.core.exceptions.ImproperlyConfigured:包含的URLconf'tutorial.urls'似乎没有任何模式。如果您在文件中看到有效的模式,则说明问题可能是由循环导入引起的
什么是循环进口?我听不懂我在创建 serializers.py 文件的过程中创建了一个名为 quickstart 的应用。
quickstart / serializers.py
from django.contrib.auth.models import User, Group
from rest_framework import serializers
class UserSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = User
fields = ('url', 'username', 'email', 'groups')
class GroupSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Group
fields = ('url', 'name')
quickstart / views.py
from django.contrib.auth.models import User, Group
from rest_framework import viewsets
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
class GroupViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows groups to be viewed or edited.
"""
queryset = Group.objects.all()
serializer_class = GroupSerializer
quickstar / urls.py
from rest_framework import routers
from .views import UserViewSet, GroupViewSet
router = routers.DefaultRouter()
router.register(r'users', views.UserViewSet)
router.register(r'groups', views.GroupViewSet)
urlpatterns = router.urls
testing / urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('', include('quickstart.urls')),
# path('api-auth/', include('rest_framework.urls', namespace='rest_framework')),
path('admin/', admin.site.urls),
]
能否请您描述为什么经常发生此错误? 谢谢!