How to save all my django applications in a specific folder

I have a Django project, say, "project1". Typical folder structure for applications:

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

What if I want to store all my applications in a separate folder, for example, applications? Thus, the structure should look like this:

/project/
         apps/
              app1/
              app2/
              ...
         __init__.py
         manage.py
         settings.py
         urls.py
+25
source share
5 answers

You can add your folder appsto your python path by pasting the following into your settings.py:

import os
import sys

PROJECT_ROOT = os.path.dirname(__file__)
sys.path.insert(0, os.path.join(PROJECT_ROOT, 'apps'))

Then you can use all the applications in this folder in the same way as in your project root directory!

+36
source

, settings.py, :

INSTALLED_APPS = (
    'apps.app1',
    'apps.app2',
    # ...
)

urls.py :

urlpatterns = patterns('', 
    (r'^app1/',include('apps.app1')),    
    (r'^app2/',include('apps.app2')),    
)

.. import,

+12

BASE_DIR, settings.py.

:

import sys
sys.path.insert(0, os.path.join('BASE_DIR', 'apps'))

, .

+2

virtualenv/virtualenvwrapper ( ), add2virtualenv python:

mkdir apps
cd apps
pwd
[/path/to/apps/dir]

, :

add2virtualenv /path/to/apps/dir
+1

__init__.py ( 4 ) .

urlpatterns = [
        path('polls/',include('apps.polls.urls')),
        path('admin/', admin.site.urls)
]
0

All Articles