带有子应用程序的 Django 路由

问题描述

我有什么:

  • 一个 Django Project,通过 Django Rest Framework 充当 REST API。
  • 许多 Django Apps 控制着我的 Postgres 数据库的逻辑。

我想做什么:

  • 使用 Robinhood 创建一个新的 Django App,代表 Service / Integration
  • 在 ^^ 内,我想在 subApplications 中构建我的逻辑,以便将 usertransactionstransfers 等的所有逻辑分开......
  • 整个 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'

我假设这是我如何将子应用程序导入整个应用程序的问题,但已添加了我能想到的所有相关代码以发现任何其他问题。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)