Home > Enterprise >  Organize django apps inside
Organize django apps inside

Time:08-15

When creating the apps you do the following: python manage.py startapp app1, but this automatically creates the app inside the root folder of the project.

/project1/
         /app1/
         /app2/
         ...
         __init__.py
         manage.py
         settings.py
         urls.py

how to create and save in a folder already created all the apps that are created in Django? for example: in this case I have a folder called "apps" inside it I will have all the apps that are created during the development time:

/project/
         apps/
              app1/
              app2/
              ...
         __init__.py
         manage.py
         settings.py
         urls.py

Anyone who can provide information would be appreciated in advance.

CodePudding user response:

As stated in the django docs,

django-admin startapp name [directory]

Creates a Django application directory structure with the specified application name, either in the current directory or in the specified destination.

For instance:

django-admin startapp myapp apps/myapp

Then you will have to slightly modify the settings file so that django recognizes the apps:

import os
import sys # new

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
...

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'myapp.apps.MyappConfig',
]
...
sys.path.append(os.path.join(BASE_DIR, 'apps')) # at the bottom of the file
  • Related