简体   繁体   中英

Django Project Directory Structure

in my django project structure, i want all my django apps in a separate Apps Folder, but when i include it in settings.py it raises an error,

raise ImproperlyConfigured( django.core.exceptions.ImproperlyConfigured: Cannot import 'TestApp'. Check that 'Apps.TestApp.apps.TestappConfig.name' is correct.

INSTALLED_APPS = [
    ...

    'Apps.TestApp'
]

But

when i only include TestApp , i raises no module named 'TestApp' Error.

INSTALLED_APPS = [
    ...

    'TestApp'
]

If you are using django version < or = 2 then you should register your app like

INSTALLED_APPS = [
   ...
 'testapp.apps.TestappConfig' 
]

the app name should not be in 'UPPER' case otherwise you will get errors.

if you are using django > or = 3 then you can register your app with it's original name too.

You are registering your app in 'Title' style which is not permitted.

You could do the following in your settings.py file:

INSTALLED_APPS = [
  ... # other necessary apps here
  
  # include your local apps you are creating for your project here
  'testapp.apps.app_name', # assuming app_name is one of your apps
  'testapp.apps.another_app',
  'testapp.apps.third_custom_app'

]

Then in each of your app folders (where your models.py, views.py, urls.py, etc. are) include an apps.py file that follows the following pattern:

from django.apps import AppConfig


class AppNameConfig(AppConfig): # note the formatting of this class name
    default_auto_field = "django.db.models.BigAutoField"
    name = "apps.app_name" # apps is the name of the directory where all your apps are located. 
                           # app_name is the name of the individual directory within your apps directory where this apps.py file is saved

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM