为什么我的 Django 项目中的 urls.py 文件不允许我导入该文件?
Why won't the urls.py file in my Django project let me import this file?
我正在尝试将另一个目录中的文件列表导入到我的 urls.py
文件中。我已将目录包含在 settings.py
文件、运行、./manage.py makemigrations
和 ./manage.py migrate
Django 命令中,并将函数和文件导入到 urls.py
文件中.
这是我当前的代码:
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')),
]
错误信息:
ImportError: cannot import name 'account_urlpatterns' from 'apps.accounts.urls' (/Users/{name}/programming/dj/backend/server/apps/accounts/urls.py)
和项目结构:
.
└── 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
非常感谢任何建议 - 提前致谢!
这是一种可行的方法。我已经使用路径来保持一致性,但这无关紧要。路径更容易使用。
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'))
]
请注意,我将 accounts_urlpatterns
更改为 urlpatterns
以使其起作用。
我正在尝试将另一个目录中的文件列表导入到我的 urls.py
文件中。我已将目录包含在 settings.py
文件、运行、./manage.py makemigrations
和 ./manage.py migrate
Django 命令中,并将函数和文件导入到 urls.py
文件中.
这是我当前的代码:
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')),
]
错误信息:
ImportError: cannot import name 'account_urlpatterns' from 'apps.accounts.urls' (/Users/{name}/programming/dj/backend/server/apps/accounts/urls.py)
和项目结构:
.
└── 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
非常感谢任何建议 - 提前致谢!
这是一种可行的方法。我已经使用路径来保持一致性,但这无关紧要。路径更容易使用。
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'))
]
请注意,我将 accounts_urlpatterns
更改为 urlpatterns
以使其起作用。