如何在Django中修复循环导入?

时间:2019-03-23 16:38:38

标签: python django rest api django-rest-framework

我正在尝试创建一个将带有电子邮件的图像上传到数据库的api。但是我收到错误消息“ raise ImproperlyConfigured(msg.format(name = self.urlconf_name))”。urls.py是否有问题?

https://imgur.com/OjPUhOv.jpg

这是我的结构外观

https://imgur.com/TW6pKPn.jpg

这是错误

用于urls.py-

from django.contrib import admin
from django.urls import path,include
from django.conf import settings

urlpatterns = [
    path('admin/', admin.site.urls),
    path('',include('user.urls')),
    path('api/',include('api_test.urls'))
    # path('articles/',include('articles.urls'))

]

对于api_test / urls.py

from django.urls import path,include
from django.conf import settings
from . import views

from rest_framework import routers

router = routers.DefaultRouter()
router.register('image_test',views.api_test,base_name='image_test')

urlpatterns = [
    # path('/',views.api_test),
    path('',include(routers.url)),


]

用于views.py

class api_test(viewsets.ModelViewSet):
    queryset = fineDB.objects.all()
    serializer_class = fineSerializer

##for serializers.py

from rest_framework import serializers
from .models import fineDB

class fineSerializer(serializers.ModelSerializer):
    image = serializers.ImageField(max_length=None,use_url=True)
    class Meta:
        model = fineDB
        fields = {'email','image'}

1 个答案:

答案 0 :(得分:0)

您可能应该从router而不是routers获取网址。 同时,您不需要该文件中的路由器和urlpatterns。您可以导入路由器并将其安装在router.urls中的urls.py中。

from rest_framework import routers

router = routers.DefaultRouter()
router.register('image_test',views.api_test,base_name='image_test')

urlpatterns = [
    # path('/',views.api_test),
    path('',include(router.urls)),    # <-


]