How to keep all my django applications in specific folder

前端 未结 7 898
逝去的感伤
逝去的感伤 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:58

    As a slight variant to Berhard Vallant's or Anshuman's answers, here is an alternative snippet to place in settings.py

    import os
    import sys  # Insert this line
    
    BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    
    # Insert the two lines below
    APPS_DIR = os.path.join(BASE_DIR, '<your_project_dir_name>/apps/')
    sys.path.insert(0, APPS_DIR)
    

    Doing it in this way has the added benefit that your template directories are cleaner as they will look like below. Without the APPS_DIR variable, there will be a lot of repitition of <your_project_dir_name>/apps/ within the DIRS list of the TEMPLATES list.

    TEMPLATES = [
        {
            'BACKEND': 'django.template.backends.django.DjangoTemplates',
            'DIRS': [
                os.path.join(APPS_DIR, '<app_name>/templates/<app_name>'),
                os.path.join(APPS_DIR, '<app_name>/templates/<app_name>'),
                ...
            ],
            'APP_DIRS': True,
            'OPTIONS': {
                'context_processors': [
                    'django.template.context_processors.debug',
                    'django.template.context_processors.request',
                    'django.contrib.auth.context_processors.auth',
                    'django.contrib.messages.context_processors.messages',
                ],
            },
        },
    ]
    

    You can list the apps within the INSTALLED_APPS list as normal with either the short-form name given in apps.py or by using the long-form syntax of appname.apps.AppnameConfig replacing appname with your app's name.

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