Home > Back-end >  Importing from views directory
Importing from views directory

Time:11-14

In my project I have an app called api where I keep my viewsets in a directory called views. I keep getting ModuleNotFoundError: No module named 'api.views.book'; 'api.views' is not a package where I import in my api/urls.py

api/urls.py

from rest_framework import routers
from api.views.book import BookViewset

router = routers.DefaultRouter()

router.register(r'books', BookViewset, 'books')

api_urls = router.urls

In my main urls.py I'm doing this:

urls.py

from django.contrib import admin
from django.urls import path
from django.conf.urls import include
from api.urls import api_urls

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

I didn't get an error until I imported api_urls

I should mention that api is included in INSTALLED_APPS

CodePudding user response:

You need to include a file named __init__.py in a directory to make it a package, then you can import from it

In your case you need a file api/views/__init__.py

  • Related