I am trying to learn the Django Rest Framework.
I create a project called djangorest.
Then I created an app called api
In djangorest/urls.py I have:
from django.contrib import admin
from django.urls import path, include
from django.conf.urls import url
urlpatterns = [
path('admin/', admin.site.urls),
path('api/', include(api.urls)),
]
In djangorest/settings.py I included the new app api:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'api.apps.ApiConfig',
]
However, when I run the server I get the error:
NameError: name 'api' is not defined
api. Addimport api.urlsin thedjangorest/urls.py.include(api.urls)you are instructing Django to include the url patterns from another object, where those patterns are taken from theurlattribute of an object namedapi. Ifapiis supposed to be a Python package (all Django apps are) then you forgot to import that package.__init__.pyfile in your api app?