简体   繁体   中英

Why won't the urls.py file in my Django project let me import this file?

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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM