1

I'm trying to import a list from a file in another directory into my urls.py file. I have included the directory in the settings.py file, ran the ./manage.py makemigrations & the ./manage.py migrate Django commands, and imported the function and the file into the urls.py file.

Here is my current code:

urls.py:

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

from apps.accounts.urls import account_urlpatterns

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

urlpatterns += accounts_urlpatterns

settings.py:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    # 
    'rest_framework',
    'rest_framework.authtoken',
    'djoser',
    #
    'apps.accounts'
]

apps/accounts/urls.py:

from django.conf.urls import url, include

accounts_urlpatterns = [
    url(r'^api/v1/', include('djoser.urls')),
    url(r'^api/v1/', include('djoser.urls.authtoken')),
]

the error message:

ImportError: cannot import name 'account_urlpatterns' from 'apps.accounts.urls' (/Users/{name}/programming/dj/backend/server/apps/accounts/urls.py)

and the project structure:

.
└── server
    ├── apps
    │   └── accounts
    │       ├── __init__.py
    │       ├── admin.py
    │       ├── apps.py
    │       ├── migrations
    │       │   └── __init__.py
    │       ├── models.py
    │       ├── tests.py
    │       ├── urls.py
    │       └── views.py
    ├── db.sqlite3
    ├── manage.py
    └── server
        ├── __init__.py
        ├── asgi.py
        ├── settings.py
        ├── urls.py
        └── wsgi.py

I appreciate any advice - thank you in advance!

logan
  • 15
  • 5
  • You say you're trying to import the function `account_urlpatterns` but it isn't a function, it is a list. This is why you are getting the error trying to import it – Danoram Jan 21 '21 at 16:34
  • @Danoram That makes sense about the list; however, the issue is that I cannot import anything from the apps.accounts directory. – logan Jan 21 '21 at 17:02

1 Answers1

1

Here is one way to do it that should work. I have used path for consistency but it shouldn't matter. path is easier to work with.

apps/accounts/urls.py

from django.urls import include, path

urlpatterns = [
    path('api/v1', include('djoser.urls')),
    path('api/v1', include('djoser.urls.authtoken'))
]

urls.py

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

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('apps.accounts.urls'))
]

Note that I changed accounts_urlpatterns to urlpatterns for this to work.

Danoram
  • 8,132
  • 12
  • 51
  • 71
  • Okay, I implemented this method, and while I like using `path` over `url`, I'm still running into the same issue of the apps.accounts directory not being able to be imported. I think it is something with the `settings.py` file, but I thought I added it correctly to the `INSTALLED_APPS` list. – logan Jan 21 '21 at 17:16
  • Try adding an empty `__init__.py` file inside the `apps` directory. – Danoram Jan 21 '21 at 17:18
  • No worries. Glad it worked. Keep up the good work. – Danoram Jan 21 '21 at 17:24