带有子应用程序的 Django 路由
Django Routing w/ subApplication
我有:
- a
Django Project
通过 Django Rest Framework 充当 REST API。
- 一些
Django Apps
控制着我的 Postgres 数据库的逻辑。
我想做什么:
- 用 Robinhood
创建一个代表 Service / Integration
的新 Django App
- 在 ^^ 我想在
subApplications
中构建我的逻辑,以便将 user
与 transactions
与 transfers
等的所有逻辑分开。 .
- 这整个
Django App
和所有 subApplications
只是 API ......他们永远不需要 models / migrations
但他们最终会与其他人交流 Django Apps
代码结构:
- 主要
Django Project
├── APP_holdings
├── APP_logs
├── APP_unique
├── APP_users
├── ⭐️⭐️ DJANGO
│ ├── __init__.py
│ ├── __pycache__
│ │ ├── __init__.cpython-38.pyc
│ │ └── wsgi.cpython-38.pyc
│ ├── asgi.py
│ ├── fixtures
│ │ ├── platforms.json
│ │ └── symbols.json
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
├── EXAMPLE.env
├── Pipfile
├── Pipfile.lock
├── Procfile
├── README.md
├── ⭐️⭐️ SERVICE_robinhood
│ ├── README.md
│ ├── __init__.py
│ ├── admin.py
│ ├── apps.py
│ ├── models.py
│ ├── tests.py
│ ├── urls.py
│ ├── ⭐️⭐️ user
│ │ ├── __init__.py
│ │ ├── admin.py
│ │ ├── api
│ │ │ ├── __init__.py
│ │ │ └── login.py
│ │ ├── apps.py
│ │ ├── urls.py
│ │ └── utils.py
│ │ ├── __init__.py
│ │ └── printSomething.py
│ └── views.py
├── manage.py
├── requirements.txt
├── static
│ └── __init__.py
└── staticfiles
SETTINGS.py
INSTALLED_APPS = [
# Pre Installed
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
# Apps
'APP_users.apps.AppUserConfig',
'APP_holdings.apps.AppHoldingsConfig',
'APP_logs.apps.AppLogsConfig',
'APP_unique.apps.AppUniqueConfig',
# !!! --- I AM ASSUMING I AM IMPORTING THE APP INCORRECTLY (and have tried many different ways mentioned on various SO threads -> See `.apps` code --- !!!
'SERVICE_robinhood.apps.ServiceRobinhoodConfig',
'SERVICE_robinhood.user.apps.UserConfig',
# Helpers
'corsheaders',
# Django Rest Framework
'rest_framework',
'rest_framework.authtoken',
]
SERVICE_robinhood/apps.py
class ServiceRobinhoodConfig(AppConfig):
name = 'SERVICE_robinhood'
SERVICE_robinhood/user/apps.py
class UserConfig(AppConfig):
# name = 'user'
name = 'SERVICE_robinhood.user'
DJANGO.urls.py
urlpatterns = [
path('admin/', admin.site.urls),
# ...auth...
path('user/', include('APP_users.urls')),
# ...otherApps...
path('RH/', include('SERVICE_robinhood.urls'))
]
SERVICE_robinhood/urls.py
# ?? do I need to import user here and pass it differently into `include` ??
# I thought that since `users` should be a module in and of itself in the overall Django Project you could just reference the app by name
urlpatterns = [
❌ path('user/', include('user.urls')),
✅ UPDATE - FROM ANSWER: ✅ path('user/', include('SERVICE_robinhood.user.urls')),
]
SERVICE_robinhood/user/urls.py
from . import utils
urlpatterns = [
path('printSomething/', utils.printSomething, name='printSomething')
]
我得到的错误:
SERVICE_robinhood/urls.py", line 23
...
ModuleNotFoundError: No module named 'user'
我假设这是我将子应用程序导入整个应用程序的方式的问题,但我已经添加了我能想到的所有相关代码以发现任何其他问题。
根据 Amine 的回答 (ProfileLink),在您的 Django App
中执行以下操作以导入 subApplications
路由:
SERVICE_robinhood.urls.py
urlpatterns = [
path('user/', include('SERVICE_robinhood.user.urls')),
]
我有:
- a
Django Project
通过 Django Rest Framework 充当 REST API。 - 一些
Django Apps
控制着我的 Postgres 数据库的逻辑。
我想做什么:
- 用 Robinhood 创建一个代表
- 在 ^^ 我想在
subApplications
中构建我的逻辑,以便将user
与transactions
与transfers
等的所有逻辑分开。 . - 这整个
Django App
和所有subApplications
只是 API ......他们永远不需要models / migrations
但他们最终会与其他人交流Django Apps
Service / Integration
的新 Django App
代码结构:
- 主要
Django Project
├── APP_holdings
├── APP_logs
├── APP_unique
├── APP_users
├── ⭐️⭐️ DJANGO
│ ├── __init__.py
│ ├── __pycache__
│ │ ├── __init__.cpython-38.pyc
│ │ └── wsgi.cpython-38.pyc
│ ├── asgi.py
│ ├── fixtures
│ │ ├── platforms.json
│ │ └── symbols.json
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
├── EXAMPLE.env
├── Pipfile
├── Pipfile.lock
├── Procfile
├── README.md
├── ⭐️⭐️ SERVICE_robinhood
│ ├── README.md
│ ├── __init__.py
│ ├── admin.py
│ ├── apps.py
│ ├── models.py
│ ├── tests.py
│ ├── urls.py
│ ├── ⭐️⭐️ user
│ │ ├── __init__.py
│ │ ├── admin.py
│ │ ├── api
│ │ │ ├── __init__.py
│ │ │ └── login.py
│ │ ├── apps.py
│ │ ├── urls.py
│ │ └── utils.py
│ │ ├── __init__.py
│ │ └── printSomething.py
│ └── views.py
├── manage.py
├── requirements.txt
├── static
│ └── __init__.py
└── staticfiles
SETTINGS.py
INSTALLED_APPS = [
# Pre Installed
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
# Apps
'APP_users.apps.AppUserConfig',
'APP_holdings.apps.AppHoldingsConfig',
'APP_logs.apps.AppLogsConfig',
'APP_unique.apps.AppUniqueConfig',
# !!! --- I AM ASSUMING I AM IMPORTING THE APP INCORRECTLY (and have tried many different ways mentioned on various SO threads -> See `.apps` code --- !!!
'SERVICE_robinhood.apps.ServiceRobinhoodConfig',
'SERVICE_robinhood.user.apps.UserConfig',
# Helpers
'corsheaders',
# Django Rest Framework
'rest_framework',
'rest_framework.authtoken',
]
SERVICE_robinhood/apps.py
class ServiceRobinhoodConfig(AppConfig):
name = 'SERVICE_robinhood'
SERVICE_robinhood/user/apps.py
class UserConfig(AppConfig):
# name = 'user'
name = 'SERVICE_robinhood.user'
DJANGO.urls.py
urlpatterns = [
path('admin/', admin.site.urls),
# ...auth...
path('user/', include('APP_users.urls')),
# ...otherApps...
path('RH/', include('SERVICE_robinhood.urls'))
]
SERVICE_robinhood/urls.py
# ?? do I need to import user here and pass it differently into `include` ??
# I thought that since `users` should be a module in and of itself in the overall Django Project you could just reference the app by name
urlpatterns = [
❌ path('user/', include('user.urls')),
✅ UPDATE - FROM ANSWER: ✅ path('user/', include('SERVICE_robinhood.user.urls')),
]
SERVICE_robinhood/user/urls.py
from . import utils
urlpatterns = [
path('printSomething/', utils.printSomething, name='printSomething')
]
我得到的错误:
SERVICE_robinhood/urls.py", line 23
...
ModuleNotFoundError: No module named 'user'
我假设这是我将子应用程序导入整个应用程序的方式的问题,但我已经添加了我能想到的所有相关代码以发现任何其他问题。
根据 Amine 的回答 (ProfileLink),在您的 Django App
中执行以下操作以导入 subApplications
路由:
SERVICE_robinhood.urls.py
urlpatterns = [
path('user/', include('SERVICE_robinhood.user.urls')),
]