打开Django服务器时出现循环导入错误

时间:2019-05-09 00:28:02

标签: python django web

当我打开服务器(Django)时,出现以下错误:“包含的URLconf'admin.urls'似乎没有任何模式。如果在文件中看到有效的模式,则可能是由于循环导入。”

我发现,通过从urls.py中删除.views导入,可以解决此问题。所以我认为问题出在views.py之内。

应用urls.py

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

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

views.py

from django.shortcuts import render
from rest_framework.response import Response
from rest_framework.views import APIView

from .models import User
from .serializers import UserSerializer

class UserView(APIView):
    def get(self, request):
        users = User.objects.all()
        serializer = UserSerializer(users, many=True)
        return Response({"users": users})

serializer.py

from rest_framework import serializers

class UserSerializer(serializers.Serializer):
    name = serializers.CharField(max_length=255)
    email = serializers.EmailField()
    password = serializers.CharField(max_length=255)
    disease = serializers.CharField(max_length=255)
    logo = serializers.TextField()

crud urls.py

在这里,问题出在第二行:如果我删除该行,我会修复错误

from django.urls import path
from .views import UserView

app_name='crud'
# app_name will help us do a reverse look-up latter.
urlpatterns = [
    path('users/', UserView.as_view()),
]

1 个答案:

答案 0 :(得分:0)

请添加错误跟踪,以更好地了解此问题。 同时尝试更改此内容:

path('/api', include('crud.urls'))

收件人:

path('api/', include('crud.urls')),

from .views import UserView

收件人:

from crud import views
urlpatterns = [
    path('users/', views.UserView.as_view()),
]