How to keep all my django applications in specific folder

前端 未结 7 897
逝去的感伤
逝去的感伤 2020-12-12 16:59

I have a Django project, let\'s say \"project1\". Typical folder structure for applications is:

/project1/
         /app1/
         /app2/
         ...
             


        
相关标签:
7条回答
  • 2020-12-12 17:36

    How about you utilize the BASE_DIR variable already present in the settings.py.

    Just add the following:

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

    Hope this helps.

    0 讨论(0)
  • 2020-12-12 17:39

    You can add your apps folder to your python path by inserting the following in 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 apps in this folder just in the same way as they were in your project root!

    0 讨论(0)
  • 2020-12-12 17:39

    Just add __init__.py (4 underscores in total) in your apps folder. Now you can just do

    urlpatterns = [
            path('polls/',include('apps.polls.urls')),
            path('admin/', admin.site.urls)
    ]
    
    0 讨论(0)
  • 2020-12-12 17:44

    In my case, my project folder structure is the following:

    /project/
       /apps/
          /app1/
          /app2/
       /src/
          /settings.py
          ...
    

    So I've solved it with these two lines on my settings.py:

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

    No need to alter urls.py.

    0 讨论(0)
  • 2020-12-12 17:55

    If you're using virtualenv/virtualenvwrapper (which is a bit dated but still valid), you can use the included add2virtualenv command to augment your python path:

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

    Copy that path to clipboard, then:

    add2virtualenv /path/to/apps/dir
    
    0 讨论(0)
  • 2020-12-12 17:58

    You can do this very easily, but you need to change the settings.py to look like this:

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

    And your urls.py to look like this:

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

    .. and modify any imports to point to the app location

    0 讨论(0)
提交回复
热议问题